- Home /
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!
Answer by b1gry4n · May 28, 2018 at 06:11 AM
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)
{
Destroy(this.gameObject);
}
_this = this;
}
public static GameManager Instance()
{
return _this;
}
which would be accessed on your "zombie script" by using
GameManager.Instance().deadZombies += 1;
I can support b1gry4n's suggestions except for the line game$$anonymous$$anager = GameObject.Find("Game$$anonymous$$anager").GetComponent<Game$$anonymous$$anager>();
Using Find() is heavy on performance. In case you have a lot of zombies getting killed at the same moment you don't want to use this.
Use the first or third suggestion.
heavy on performance yeh, but depending on his setup it might work for him. if youre doing it at the start of the game only one time you wont notice a performance hit. if youre doing it multiple times an update, yes you will notice
Yeah and if let's say 1000 zombies get inited in the same Initial Update you will notice as well ;)
Either way it can lead to unwanted game-jittering. But yeah in the end it is ofc a valid solition :)
Answer by SmokiestSpace6 · May 31, 2018 at 12:35 AM
Thanks this is exactly what I needed!
@SmokiestSpace6 don't forget to mark your question as answered.
Answer by Nerull22 · May 31, 2018 at 03:57 AM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612163542im_/https://answers.unity.com/themes/thub/images/avi.jpg)