How to access the same variable in C# from multiple different prefabs

I’m creating a zombie game and spawning in zombies all with the same script with the bool variable “dead” and i want to make it so that if it is true for any of the zombies it tells the game manager that a zombie has died (for enemy count)

I tried to get the game object “Enemy” from the scene and then get the script from it and check the “dead” bool in the function Update but it did not work for the spawned zombies

Hopefully all that made sense, and thanks for all the help!

theres a few ways you can do this. when you spawn your zombie, give the zombie the game manager and have the zombie store the reference until it needs it

            GameObject zombie = (GameObject)Instantiate(zombiePrefab, zombieSpawnpoint, Quaternion.identity);
            ZombieScript zscript = zombie.GetComponent<ZombieScript>();
            zscript.gameManager = this;

and then when your zombie dies…

  gameManager.deadZombies += 1;

or you can have the zombie find the game manager…

gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();

or you can look into making the game manager a static object

public static GameManager _this;
    private void Awake()
        if (GameManager.Instance() != null)

        _this = this;

    public static GameManager Instance()
        return _this;

which would be accessed on your “zombie script” by using

 GameManager.Instance().deadZombies += 1;

Thanks this is exactly what I needed!

In your zombie class, you could also have a static variable that maintains the count.

public static int ZombieCount = 0;

In the awake method, you could increment it. And the ‘OnDestroy’ Method you could decrement.

Or you could manage it via a property, similar to an event.

It seems like a far easier solution than building a whole Singleton object for the count. This is easier to maintain and have more ability to keep your objects encapsulated and not dependent on other objects existing in your scene for them to function properly. I’ve done this many times in the past and often prefer it over a singleton pattern.