# Variable wont update

**URL:** https://discussions.unity.com/t/variable-wont-update/418165
**Category:** Scripting
**Tags:** Scripting
**Created:** [July 15, 2010, 12:41am UTC](https://discussions.unity.com/t/variable-wont-update/418165 "2010-07-15T00:41:03Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![FreakFish](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/freakfish/32/280338_2.png) [@FreakFish](https://discussions.unity.com/u/FreakFish)
#### Post date: [July 15, 2010, 12:41am UTC](https://discussions.unity.com/t/variable-wont-update/418165/1 "2010-07-15T00:41:03Z")

</div>

Good morning everybody, it’s me freakfish and i’m back with another annoying problem courtesy of the Unity3D engine! Today we have an issue with a variable that is equal to the transform.position.x of the gameobject it is attached to - it is simply refusing to update even though i’ve told it to firmly in the update function. Here is the script in question;

```auto
static var move = -0.5;
static var moveup = -0.5;
static var cowxpos : float;
static var cowypos : float;

function Update () 
{ 
	cowxpos = transform.position.x; 
	cowypos = transform.position.y;
	transform.position.z = -0.5;
	// instead of changing the value of move here every frame, let the other script dictate it -
	// aka an if statement asking whether or not the beam is on, which will change the horizontal
	// move and vertical move speeds, and an else statement that does the same appropriately
	
	if (transform.position.x < -11.4) 
	{ 
	transform.rotation.y = 180; 
	} 
	if (transform.position.x > 3.2) 
	{ 
	transform.rotation.y = 0; 
	} 
	if (transform.position.y < -4.0001)
	{
	transform.position.y = -4;
	}
	
	move *= Time.deltaTime; // this is changing the value of move - thats why the cow wont
	moveup *= Time.deltaTime; // move unless it's reset every time it's run.

	transform.Translate (move, moveup, 0); 
	
}

```

this is a right pain in the arse. there doesnt seem to be a reason why its not working, for some reason the “cowxpos” variable just isn’t updating, and as a result my UFO can’t beam up the cow unless it hovers over it’s original x position.

please, HALP! ☹

---

<div class="post-metadata">

### Author: ![dart](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dart/32/243762_2.png) [@dart](https://discussions.unity.com/u/dart)
#### Post date: [July 15, 2010, 3:00am UTC](https://discussions.unity.com/t/variable-wont-update/418165/2 "2010-07-15T03:00:18Z")

</div>

Try this

```auto
  var vel : int = 3;

   ...

   move = move + vel * Time.deltaTime;  
   moveup = moveup + vel * Time.deltaTime; 

   ...

```

Time.deltaTime is small, and when you multiply the variable to it along the frames, it gets even smaller as the simulation goes on. Multiply it to a vel and then summ to your variable.

---

<div class="post-metadata">

### Author: ![FreakFish](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/freakfish/32/280338_2.png) [@FreakFish](https://discussions.unity.com/u/FreakFish)
#### Post date: [July 15, 2010, 3:37am UTC](https://discussions.unity.com/t/variable-wont-update/418165/3 "2010-07-15T03:37:50Z")

</div>

no, youve misunderstood -  
where it says cowxpos = transform.position.x

its not doing it. thats what i need to fix, everything else is fine. the variables for speed are referenced from another script, that doesnt need any fixing 🙂

for some reason, cowxpos does not equal transform.position.x , it just equals the original co-ordinate then stays like it, even though i’ve told it to update every frame

---

<div class="post-metadata">

### Author: ![tonyd](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/tonyd/32/437055_2.png) [@tonyd](https://discussions.unity.com/u/tonyd)
#### Post date: [July 15, 2010, 10:16am UTC](https://discussions.unity.com/t/variable-wont-update/418165/4 "2010-07-15T10:16:27Z")

</div>

> [@""](#):
>
> Good morning everybody, it’s me freakfish and i’m back with another annoying problem courtesy of the Unity3D engine!

Starting your posts by insulting Unity probably isn’t the best way to encourage people to help you.

> [@](#):
>
> Today we have an issue with a variable that is equal to the transform.position.x of the gameobject it is attached to - it is simply refusing to update even though i’ve told it to firmly in the update function.

My guess is, you’re using a static variable and have these scripts attached to more than one game object. You can’t do that.

Static variables should only be used for things like your score, in a master script that you have one instance of.

---

<div class="post-metadata">

### Author: ![Chris-Sinclair](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/chris-sinclair/32/241371_2.png) [@Chris-Sinclair](https://discussions.unity.com/u/Chris-Sinclair)
#### Post date: [July 15, 2010, 1:28pm UTC](https://discussions.unity.com/t/variable-wont-update/418165/5 "2010-07-15T13:28:09Z")

</div>

Try outputting (using the “print” method) the values of transform.position.x at the start of the method, and the value of “move” before you make your final call to transform.Translate(). Place print statements checking your variable and transform information and validating it against what you logically expect it to be.

And like suggested above, unless this is the only script using it, you might want to make your move/moveup/cowxpos/cowypos non-static.

Also as noted: 99.9999999% of the time it’s not the fault of the engine. The engine is stupid and will do exactly what you tell it to do. 😄

---

<div class="post-metadata">

### Author: ![anon\_91920111](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anon\_91920111](https://discussions.unity.com/u/anon_91920111)
#### Post date: [July 15, 2010, 1:32pm UTC](https://discussions.unity.com/t/variable-wont-update/418165/6 "2010-07-15T13:32:39Z")

</div>

If it was really Unity which sometimes happens then this works.

1 detach your script  
2 run the game  
3 fix any errors in the script  
4 reattach the script

---

<div class="post-metadata">

### Author: ![anon\_14800654](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anon\_14800654](https://discussions.unity.com/u/anon_14800654)
#### Post date: [July 15, 2010, 3:10pm UTC](https://discussions.unity.com/t/variable-wont-update/418165/7 "2010-07-15T15:10:40Z")

</div>

> [@""](#):
>
> My guess is, you’re using a static variable and have these scripts attached to more than one game object. You can’t do that.
> 
> Static variables should only be used for things like your score, in a master script that you have one instance of.

@tonyd - So in JS, does declaring a var as static imply a kind of singleton? Is this an “easier” method of getting singleton type behaviour?

I am still a little unclear on singleton usage, creation etc.

---

<div class="post-metadata">

### Author: ![tonyd](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/tonyd/32/437055_2.png) [@tonyd](https://discussions.unity.com/u/tonyd)
#### Post date: [July 15, 2010, 3:48pm UTC](https://discussions.unity.com/t/variable-wont-update/418165/8 "2010-07-15T15:48:34Z")

</div>

Static variables are simply global variables.

I don’t use Singletons, but I think the only difference between a Singleton and a static var is that a Singleton ensures that you can only have one instance of it, where as with a static var you have to remember not to create a second instance of it, else you’ll get strange behavior.

Someone correct me if I’m wrong…

---

<div class="post-metadata">

### Author: ![FreakFish](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/freakfish/32/280338_2.png) [@FreakFish](https://discussions.unity.com/u/FreakFish)
#### Post date: [July 16, 2010, 12:24am UTC](https://discussions.unity.com/t/variable-wont-update/418165/9 "2010-07-16T00:24:19Z")

</div>

the script is only currently attached to one cow, and thanks for the warning - i was worried that may be a problem. im just going to have to create new variables for each cow, but thats not so much of a problem.

i tried printing the value of cowxpos, turns out its just getting lower and lower very slowly for some reason, like -0.01 every 10 seconds. this just doesnt make any sense to me D: as far as i can see i haven’t given it any commands that could possibly make it get smaller, and its not dividing because it starts positive and goes into negative - that rules out the time.deltaTime.

if i’ve told it to equal its own x position why is it just getting lower? ☹ it moves from left to right, so its definately not accurate - i thought it may be to do with the objects being grouped by the heighrachy but it still doesnt seem to make any sense D: can anybody think of a reason why this is happening?

---

<div class="post-metadata">

### Author: ![FreakFish](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/freakfish/32/280338_2.png) [@FreakFish](https://discussions.unity.com/u/FreakFish)
#### Post date: [July 16, 2010, 1:15pm UTC](https://discussions.unity.com/t/variable-wont-update/418165/10 "2010-07-16T13:15:39Z")

</div>

bump

---

<div class="post-metadata">

### Author: ![tomvds](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@tomvds](https://discussions.unity.com/u/tomvds)
#### Post date: [July 16, 2010, 1:33pm UTC](https://discussions.unity.com/t/variable-wont-update/418165/11 "2010-07-16T13:33:05Z")

</div>

> [@""](#):
>
> if i’ve told it to equal its own x position why is it just getting lower? ☹ it moves from left to right, so its definately not accurate - i thought it may be to do with the objects being grouped by the heighrachy but it still doesnt seem to make any sense D: can anybody think of a reason why this is happening?

Are you sure your camera is facing the world forward axis? Your ‘move’ variable starts out negative and is used to translate the cow, which will then reduce the x coordinate every frame, moving it to the left when looking at it along the world forward axis. However, with an opposite camera angle, it will be moving to the right.

---

<div class="post-metadata">

### Author: ![FreakFish](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/freakfish/32/280338_2.png) [@FreakFish](https://discussions.unity.com/u/FreakFish)
#### Post date: [July 16, 2010, 1:52pm UTC](https://discussions.unity.com/t/variable-wont-update/418165/12 "2010-07-16T13:52:12Z")

</div>

i mean repeating, it wanders like any old cow in and old 2d alien abduction game lol - the move variable is calculated by another script, namely CowControl, thats fine. cowxpos just isnt equalling transform.position.x for some reason, even though its the first thing i’ve told it to do in the script ☹

---

<div class="post-metadata">

### Author: ![anon\_51720730](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@anon\_51720730](https://discussions.unity.com/u/anon_51720730)
#### Post date: [July 16, 2010, 1:59pm UTC](https://discussions.unity.com/t/variable-wont-update/418165/13 "2010-07-16T13:59:23Z")

</div>

I don’t really understand the problem… however, if you multiply a decimal value (move) by another decimal value (Time.deltaTime), you’ll get another decimal value.

Example:

0.5 \* 0.5 equals 0.25, **not** 2.5.

So, you might be experiencing the problem that the “move” variable (-0.5) get’s multiplied by deltaTime (let’s assume 0.03) each frime, so:

-0.5 \* 0.03 = -0.015

Next frame:

-0.015 \* 0.03 = -0.000225

And so forth…

If you want something to move, you have to set up a “speed” variable, multiply the “speed” for deltaTime, and add that result (deltaTime \* speed) to the move variable.

Example:

```auto
var speed : float = 20.0;
move = speed * Time.deltaTime;

```

I hope this helps.

---

<div class="post-metadata">

### Author: ![FreakFish](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/freakfish/32/280338_2.png) [@FreakFish](https://discussions.unity.com/u/FreakFish)
#### Post date: [July 16, 2010, 2:05pm UTC](https://discussions.unity.com/t/variable-wont-update/418165/14 "2010-07-16T14:05:16Z")

</div>

this is nothing to do with the cow’s movement - the variable **cowxpos** is meant to equal **transform.position.x** every frame, but it doesnt. i suspect the reason that nobody is really getting the problem is because its just not right =/ this shouldn’t be happening!

---

<div class="post-metadata">

### Author: ![tomvds](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@tomvds](https://discussions.unity.com/u/tomvds)
#### Post date: [July 16, 2010, 2:24pm UTC](https://discussions.unity.com/t/variable-wont-update/418165/15 "2010-07-16T14:24:51Z")

</div>

> [@""](#):
>
> this is nothing to do with the cow’s movement - the variable **cowxpos** is meant to equal **transform.position.x** every frame, but it doesnt. i suspect the reason that nobody is really getting the problem is because its just not right =/ this shouldn’t be happening!

You are translating the cow in this very script, in the transform.Translate line. If you want cowxpos to have the same value as the transform shows in the editor (in paused mode), you have to print the value after the translate, not before.

If you also have another script independently translating the cow, then its movement behaviour is undefined. There is no way of knowing in which order the update functions will be executed, so there is no way of knowing whether or not cowxpos will match the x value of the transform at the end of the frame.

---

<div class="post-metadata">

### Author: ![tonyd](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/tonyd/32/437055_2.png) [@tonyd](https://discussions.unity.com/u/tonyd)
#### Post date: [July 16, 2010, 2:28pm UTC](https://discussions.unity.com/t/variable-wont-update/418165/16 "2010-07-16T14:28:25Z")

</div>

I just tested your code and transform.position.x does indeed equal cowxpos.

Place the following code right after the line you set cowxpos and look at the results:

```auto
Debug.Log(transform.position.x + ", " + cowxpos);

```

It the two numbers don’t match on your end, you likely have another script changing the cowxpos variable.

---

<div class="post-metadata">

### Author: ![FreakFish](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/freakfish/32/280338_2.png) [@FreakFish](https://discussions.unity.com/u/FreakFish)
#### Post date: [July 16, 2010, 11:42pm UTC](https://discussions.unity.com/t/variable-wont-update/418165/17 "2010-07-16T23:42:44Z")

</div>

i’ve been such a fool… my daily 1-4am haste has caused me to be blind. i was focusing so much on the cowcontrol script i didn’t notice that the cowmove script was on another cow, or at least didn’t think it was a problem.

i’m so sorry for wasting your time D: on the bright side, its solved though 😄
