Add Currency OnDamage() Script

Im trying to reward 10 currency points to my character everytime he hits an enemy, in the "Character Health" component there is an event section where i can give points. So ive tried this script and gave it to my zombies:

using UnityEngine; namespace Opsive.UltimateInventorySystem.Core.InventoryCollections { using Opsive.Shared.Utility; using Opsive.UltimateInventorySystem.Core.DataStructures; using Opsive.UltimateInventorySystem.Exchange; using Opsive.UltimateInventorySystem.ItemActions; using Opsive.UltimateInventorySystem.Storage; public class PointsManager : MonoBehaviour { public static PointsManager instance; public CurrencyOwner CurrencyOwner; private void Awake() { instance = this; CurrencyOwner = GetComponent<CurrencyOwner>(); } public void addPoints() { var points = InventorySystemManager.GetCurrency("Points"); CurrencyOwner.CurrencyAmount.AddCurrency(points, 10); } public void zombieDeadAddPoints() { var points = InventorySystemManager.GetCurrency("Points"); CurrencyOwner.CurrencyAmount.AddCurrency(points, 60); } } }

I assigned the functions to the Events OnDamage() and OnDeath() it works perfectly, WHEN the Zombie is directly in the scene,

The Problem: My Zombies are Spawned() with Pooling which erases the GameObject when i assign it

This means, i have to find a way to recognize the Currency Owner frrom a scrript that is attached in the Zombiie Prefab


I tried my best but im breaking my head now, xD please help
 
I was being a dumb potatoe! Let me share with you guys so if anyone is trying to replicate CoD Zombies reward point system you'll be able to :D

Im using 2 scripts:

Points Manager:

C#:
namespace Opsive.UltimateInventorySystem.Core.InventoryCollections
{
    using UnityEngine;
    using Opsive.UltimateInventorySystem.Exchange;
    
    public class PointsManager : MonoBehaviour
    {

        public CurrencyOwner CurrencyOwner;

        public void addPoints()
        {
            var points = InventorySystemManager.GetCurrency("Points");
            CurrencyOwner.CurrencyAmount.AddCurrency(points, 10);
        }

        public void zombieDeadAddPoints()
        {
            var points = InventorySystemManager.GetCurrency("Points");
            CurrencyOwner.CurrencyAmount.AddCurrency(points, 60);
        }
    }
}

Assigned to my Character

and i gave the Zombies, Which are spawned as prefabs, this AddPoints script:

C#:
namespace Opsive.UltimateInventorySystem.Core.InventoryCollections
{
    using Opsive.UltimateInventorySystem.Exchange;
    using UnityEngine;

    public class AddPoints : MonoBehaviour
    {


        public void addTenPoints()
        {
            FindObjectOfType<PointsManager>().addPoints();
        }

        public void addSixtyPoints()
        {
            FindObjectOfType<PointsManager>().zombieDeadAddPoints();
        }
    }
}

Finally call this Functions when the Zombies receive damage, this AddPoints Script is added as a component of the Zombie prefab
 
This is the errorr itss giving:
NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateInventorySystem.Core.InventoryCollections.PointsManager.addPoints () (at Assets/Scripts/PointsManager.cs:15)
Opsive.UltimateInventorySystem.Core.InventoryCollections.AddPoints.addTenPoints () (at Assets/Scripts/AddPoints.cs:10)

Im failing to Reference the Currency Owner?

I have Added the "Character" component that has the "Currency owner" to the GameObject that is using the PointsManager

Please help :)
 
YESS I got it fixed, I had to Assign the PointsManager to the "Character" gameobject, which also has the "CurrencyOwner" component. which i was trying to reference :D Im learning so much
 
I'm glad you got it fixed!

Just a little tip. FindObjectOfType<T>() is not very efficient. It is fine since you are not using it everyframe, but it's good practice to cache that value.

You can do so in an OnEnable() or Awake function.

By caching I mean something like this (code untested):

Code:
private PointsManager m_PointsManager;

public void Awake(){
    m_PointsManager = FindObjectOfType<PointsManager>();
}

// Use m_PointsManager from there onwards.

One other tip, you created your script in the Ultimate Inventory System namspace. That's bad practice, you should create your scripts in your own folders with your own namespaces. Otherwise if we ever create a script with that name in the future your script will be overwritten by ours in an update.


PS: you might be interested in learning about Singleton and ToolBox patterns:

Make sure to read carefully as they are powerful but can bite you back if overused. The ToolBox is a good way around the issues caused by too many singleton overuse, but it is not perfect.
 
Top