- Home /
Finding value of an int?
Hi guys,
I'm trying to read the value of the StuffCollected int so that when it reaches 25 the level will change. Not too sure about it and could use a nudge in the right direction. Here's what I have so far, not sure where to go from here.
Cheers.
static var StuffCollected : int = 0;
function Update () {
guiText.text= "Stuff: " + StuffCollected;
if (StuffCollected == 25){
Application.LoadLevel ("Level2");
}
}
I would recommend removing that code from Update, since Update runs every frame, and there's no reason to change the guiText.text or check stuffCollected every frame. Just make a function that does that, and call the function when needed.
Answer by aldonaletto · Feb 03, 2012 at 01:45 AM
This code seems ok - are you having any problem? Anyway, be aware that a static variable is created and initialized when the program begins, and survive till the end of the program. Due to this, its value will still be 25 when Level2 is loaded, thus you must zero it at Start or Awake if you plan to use it again.
The script above must be attached to the GUIText object, and you can increment SuffCollected in other scripts using something like this:
GuiScript.StuffCollected += 1;
where GuiScript is the above script's name.
Bizarre, it works now, but I haven't changed anything. $$anonymous$$aybe I was doing something stupid. Thanks for the help guys :)
Your answer
Follow this Question
Related Questions
Scoring bug error with 2 int variables 0 Answers
New scene for minigame or not? 1 Answer
Keeping track of time after calling Application.LoadLevel 1 Answer
How come when I restart my level it doesn't work? 2 Answers
CHaracter walking through walls 3 Answers