# Variable Updating Issue

**URL:** <https://discussions.unity.com/t/variable-updating-issue/394607>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [July 9, 2009, 3:41am UTC](https://discussions.unity.com/t/variable-updating-issue/394607 "2009-07-09T03:41:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brad-Keys](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/brad-keys/32/360918_2.png) [@Brad-Keys](https://discussions.unity.com/u/Brad-Keys)\
**Post date:** [July 9, 2009, 3:41am UTC](https://discussions.unity.com/t/variable-updating-issue/394607/1 "2009-07-09T03:41:52Z")

</div>

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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![anon\_33263724](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@anon\_33263724](https://discussions.unity.com/u/anon_33263724)\
**Post date:** [July 9, 2009, 10:07am UTC](https://discussions.unity.com/t/variable-updating-issue/394607/2 "2009-07-09T10:07:43Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Brad-Keys](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/brad-keys/32/360918_2.png) [@Brad-Keys](https://discussions.unity.com/u/Brad-Keys)\
**Post date:** [July 13, 2009, 5:32am UTC](https://discussions.unity.com/t/variable-updating-issue/394607/3 "2009-07-13T05:32:22Z")

</div>

> [@""](#):
>
> 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.

I thought so.

> [@""](#):
>
> 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).

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

---

<div class="post-metadata">

**Author:** ![beezir](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/beezir/32/567312_2.png) [@beezir](https://discussions.unity.com/u/beezir)\
**Post date:** [July 13, 2009, 6:03am UTC](https://discussions.unity.com/t/variable-updating-issue/394607/4 "2009-07-13T06:03:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon\_33263724](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@anon\_33263724](https://discussions.unity.com/u/anon_33263724)\
**Post date:** [July 13, 2009, 8:14am UTC](https://discussions.unity.com/t/variable-updating-issue/394607/5 "2009-07-13T08:14:48Z")

</div>

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”…)

---

<div class="post-metadata">

**Author:** ![Brad-Keys](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/brad-keys/32/360918_2.png) [@Brad-Keys](https://discussions.unity.com/u/Brad-Keys)\
**Post date:** [July 13, 2009, 9:21am UTC](https://discussions.unity.com/t/variable-updating-issue/394607/6 "2009-07-13T09:21:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![beezir](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/beezir/32/567312_2.png) [@beezir](https://discussions.unity.com/u/beezir)\
**Post date:** [July 13, 2009, 6:44pm UTC](https://discussions.unity.com/t/variable-updating-issue/394607/7 "2009-07-13T18:44:12Z")

</div>

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.
