x


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");

       }
}
more ▼

asked Feb 03 '12 at 01:21 AM

Pr3Z gravatar image

Pr3Z
16 2 5 8

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.

Feb 03 '12 at 02:24 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Feb 03 '12 at 01:45 AM

aldonaletto gravatar image

aldonaletto
41.2k 16 42 195

Bizarre, it works now, but I haven't changed anything. Maybe I was doing something stupid. Thanks for the help guys :)

Feb 09 '12 at 11:31 PM Pr3Z
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x113
x76
x66

asked: Feb 03 '12 at 01:21 AM

Seen: 288 times

Last Updated: Feb 09 '12 at 11:31 PM