- Home /
Static variable issue in published
Hey guys, so I can manipulate static variables in a class that holds all my between scene information (such as which controller is controlling which unit etc.) However, this functionality only exists when play the game in development, as soon as I publish it, it seems to no longer work, as soon as I get to a line of code that tries to manipulate the static variables, I.e.:
stats.Controllers[0] = 1;
(Here I'm accessing the class which has the static variable, and adjusting it so controller[0] is searching for input from input device 1.)
The code just gets ignored for the rest of the section, if I have lines after it they are not called at all.
Does anyone have a possible solution?
Answer by Owen-Reynolds · Feb 07, 2012 at 03:06 AM
I'm guessing the same way that Unity doesn't show static vars in the Inspector, it doesn't like to save them, either. Probably Controllers[0]
is giving you a null, since Controllers wasn't created.
Instead of static, you can make them normal public vars, and put the stats
script on an empty (which Instantiates them.) You can now set them in the Inspector, they get saved the usual way, etc... . To use them: GameObject.Find("statObject").GetComponent<stats>().Controllers[0]
. Or the usual where you grab a reference:
stats S; // global
S=GameObject.Find("statObject").GetComponent<stats>(); // in Start
S.Controllers[0]... // to use
Technically, Patterns people would probably call that a Singleton.
I certainly understand what you mean, the only issue is how to get it to persist between scenes and be accessible in each, how could this be achieved?
There's a function in Object DontDestroyOnLoad(transform.gameObject);
which tells something to persist between Scenes. It's pretty much made for this. Hard to find -- think it's in the HowToLoadLevels part of the manual.
still doesn't work, changed it all to what you said and it does nothing in published version, but works fine in normal version.
the code looks like this:
if (Input.GetButtonDown(slotButton + slot1Cont) && Time.time > inputTimer1)
{
if (slot1Cont != null)
stats.Controllers[0] = int.Parse(slot1Cont);
if (slot2Cont != null)
stats.Controllers[1] = int.Parse(slot2Cont);
if (slot3Cont != null)
stats.Controllers[2] = int.Parse(slot3Cont);
if (slot4Cont != null)
stats.Controllers[3] = int.Parse(slot4Cont);
Application.LoadLevel("Building"); // load the game level.
}
If I put the Application.Load level before the block of ifs, it works, if I don't, it doesn't.
stats.Controllers
is for the version using static. If Controllers
is normal (non-static,) then it should cause an error. For example, if hitPoints is in orcScript you can say orc1.hitPoints
, orc2.hitPoints
but you can't say orcScript.hitPoints
(why would you want to? which orc is that?)
If this is in the stats script, just use the var name: Controller
. If not, look up the script through the game object (above.)
Your answer
Follow this Question
Related Questions
Global Variables Refuse to Cooperate 1 Answer
Not asigned health script 1 Answer
Changing static variables from another script? 1 Answer
My static variable is not changing 2 Answers