Why does the Wood value not getting anything added to it?

I am trying to have the script give me 100 of the wood value when i break this box, and have it display in the text UI. I got the box to disappear when it runs out of health, so the if statement clearly works, but i cannot for the life of me get it to add 100 wood to the display. I feel like i am missing something super obvious. (Also, i used the tutorial from unity that was how to make a gun using raycasts, and this shootable box script was part of it - Let's Try: Shooting with Raycasts - Unity Learn) Thanks for taking a look at this!

public class ShootableBox : MonoBehaviour {

public int currentHealth = 3;
public UnityEngine.UI.Text materialDisplay;
public int wood = 0;
public int woodGiven = 100;

void Update()
{
    materialDisplay.text = "Wood: " + wood;
}

public void Damage(int damageAmount)
{

    currentHealth -= damageAmount;

    if (currentHealth <= 0)
    {
        wood = wood + woodGiven;

        gameObject.SetActive(false);
    }
    
}

}

Two issues…

  • Youre turning off the gameobject,
    which disables the script on that
    gameobject. Update wont run on
    disabled scripts.
  • Your “wood” value should be stored on
    some sort of manager script. If you
    have multiple crates, each with their own script trying to keep track
    of 1 value and they dont know
    anything about each other, how can you
    expect consistent results?

When your crate dies and you transfer wood it should update the “resource manager” script and add wood to it. There are many ways to do that from SendMessage to unity events to findobjects to static functions. If keeping track of the resources isnt important to you, you can just do this…

        if (currentHealth <= 0)
        {
            int woodVal = int.Parse(materialDisplay.text);
            woodVal += woodGiven;
            materialDisplay.text = woodVal.ToString();
            gameObject.SetActive(false);
        }

But you would have to not have “Wood:” in the materialdisplay.text if youre going to parse, which is just another reason to create a resource manager script.

Seriously though, create a static resource manager. You could then simply say

ResourceManager.GiveWood(woodGiven);

And your script would be something like…

    public class ResourceManager : MonoBehaviour
    {
        public int wood;
        public UnityEngine.UI.Text materialDisplay;
        private static ResourceManager instance;
        void Awake()
        {
            if (Instance() == null)
            {
                instance = this;
            }
        }
        public static ResourceManager Instance()
        {
            return instance;
        }
        public static void GiveWood(int amt)
        {
            instance.wood += amt;
            instance.materialDisplay.text = "Wood: " + instance.wood;
        }
    }

That would cut out the crate having any control over any UI elements or resource management. All it would do is manage its own HP, die, and update the resources. Your crate script would be simplified to this…

    public class ShootableBox : MonoBehaviour
    {
        public int currentHealth = 3;
        public int woodGiven = 100;

        public void Damage(int damageAmount)
        {
            currentHealth -= damageAmount;
            if (currentHealth <= 0)
            {
                ResourceManager.GiveWood(woodGiven);
                gameObject.SetActive(false);
            }
        }
    }