- Home /
accessed to a static variable but not getting it's value.
script #1
public class StageManager : MonoBehaviour {
public static bool stageActive = true;
}
script #2
public class SpawnManager : MonoBehaviour {
void Update () {
if (StageManager.stageActive) {
SpawnEnemy();
}
}
}
in sciript #2, SpawnEnemy() is not operating and it doesn't make any error message. Am I doing it wrong in calling static variable? :( or should I call #1 with GetComponent?
Can you print something in SpawnEnemy? Also, this is going to spawn enemy each frame so you get 50 to 100 of them per second.
To add to what fafase said you shouldn't need to use GetComponent. Also, make sure you have your Stage$$anonymous$$anager script attached to a gameObject. Static variables will only work if there is at least one instance of the class in the scene.
That's nonsense. Static variables are not bound to any instance at all. They do not belong to an instance of a class. They belong to the class itself (or to the static context of that class). Whenever you access a class type for the first time the static field initializers and the static constructor will be called automatically. At this point you do not have a single instance of the class.
It's much more important to have the Spawn$$anonymous$$anager actually attached to an active object in the scene.
Of course since Stage$$anonymous$$anager is derived from $$anonymous$$onoBehaviour it seems to be intended as component. though at the moment it doesn't have any instance members so it's irrelevant if you attach it to an object or not.
You can check this in all of 10 seconds: http://rextester.com/HWSSY24326
SpawnEnemy
could be empty or behaving normally. Can you at least post what it does?
Answer by FlippingFlopper · May 01, 2018 at 01:00 PM
After few hours of sticking in front of monitor to check my scripts, I found out that It was looping from other scripts which is making stageActive variable to be false. So I learned that frequent usage of static variable can be problem if I can't remember where all of them are :p. Thank you for your replies, guys !.