Variable Updating Issue

There are two players in my networked game, they both have health. I’m trying to display the player’s own health (not their opponent’s) in a networked game.

In my OnCollisionEnter function, when I use the Debug.Log to output the current value of the player’s health, it’s correct.

In my OnGUI when I use Debug.Log to output the current value of the player’s health, it’s always 100.

private var health : int = 100;

function OnGUI() {
        // *PROBLEM* health is always 100, should change
	Debug.Log("Health: " + health);
}

function OnCollisionEnter(collision : Collision) {
	health -= 10;
        // *CORRECT* Health value changes 
	Debug.Log(health);
}

Any idea what’s wrong?

If the script is written just as you posted it, both functions on the SAME INSTANCE of the script, without any other references to “health” what-so-ever, it should work just fine.

Any chance you’re observing this script in your networkView and trying to transmit the health to the other player or to the server? (Even though you’re not gonna display it, like you say).

I thought so.

No, I’m only observing the transform of an object.

Odd, seems to work fine here. Is health referenced anywhere else in the script (or in other scripts) at all right now?

Did you do copy-paste to your code?
What I mean is… I bet you have GUI code in your OnGUI function, and more lines of code that could be the problem, so we can’t really see if there is some kind of mistake since you re-wrote it in order to post it here.
Any chance you can post your script as-is? (And any script that is referencing “health”…)

That is the exact code. I stripped it down to the problem, executed it just how it is there and it still gives me the problem. It’s extremely odd. I’ll post the project package on here soon if you’d like.

I’d be happy to take a look at the package. If the snippet of code above gives you trouble on its own, I’m really not sure what the issue would be. I cut and pasted it just to make sure it worked, and it did.