# Variable value inside Update()

Hi folks,

I’m new to programming, and I’m bit lost with something :

I’ve got a var speed, which is set to x.
I save the x value inside another var save.
I then want to give another value to my var speed, for instance y.
Then, I want to use the former value, by setting my var speed equal to the var save.

The concern is that var speed is still equal to y.

What am I doing wrong?

I think that it’s because I’m working in the Update() method, and it’s frame related, but I don’t know how to work around.

`````` void Update()
{
GameObject model = GameObject.Find("Model");
float speedX = speed;

//Get inputs
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");

//Player Character grounded
if (Physics.Raycast(transform.position, Vector3.down, 1f))
{
//Move
transform.Translate(new Vector3(moveH, 0, moveV) * Time.deltaTime * speed);
}
//Player Character !grounded
else
{
//Move + Fall
transform.Translate(new Vector3(moveH, -1, moveV) * Time.deltaTime * speed);
if (model.transform.rotation.x < 0.50)
{
model.transform.Rotate(new Vector3(120, 0, 0) * Time.deltaTime, Space.World);
}
else if (model.transform.rotation.x > 0.50)
{
model.transform.Rotate(new Vector3(0, 0, 0));
if (Input.GetKey(KeyCode.Space))
{
if (model.transform.rotation.x < 0.90)
{
model.transform.Rotate(new Vector3(120, 0, 0) * Time.deltaTime, Space.World);
speed = 15;
}
}
else
{
if (model.transform.rotation.x > 0.50)
{
model.transform.Rotate(new Vector3(-120, 0, 0) * Time.deltaTime, Space.World);
speed = speedX;
}
}
}
}
}
``````

Have you tried putting Debug.Log’s in every place where you modify speed, to see if it’s being changed when you think it’s being changed? (be sure ‘Collapse’ is off int he console, as well)

You need another variable to defaultSpeed;

``````float defaultSpeed = 15; //your first variable
void Update()
{
GameObject model = GameObject.Find("Model");
float speedX = defaultSpeed;
//Get inputs
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
//Player Character grounded
if (Physics.Raycast(transform.position, Vector3.down, 1f))
{
//Move
transform.Translate(new Vector3(moveH, 0, moveV) * Time.deltaTime * speed);
}
//Player Character !grounded
else
{
//Move + Fall
transform.Translate(new Vector3(moveH, -1, moveV) * Time.deltaTime * speed);
if (model.transform.rotation.x < 0.50)
{
model.transform.Rotate(new Vector3(120, 0, 0) * Time.deltaTime, Space.World);
}
else if (model.transform.rotation.x > 0.50)
{
model.transform.Rotate(new Vector3(0, 0, 0));
if (Input.GetKey(KeyCode.Space))
{
if (model.transform.rotation.x < 0.90)
{
model.transform.Rotate(new Vector3(120, 0, 0) * Time.deltaTime, Space.World);
speed = 15;
}
}
else
{
if (model.transform.rotation.x > 0.50)
{
model.transform.Rotate(new Vector3(-120, 0, 0) * Time.deltaTime, Space.World);
speed = speedX;
}
}
}
}
}
``````

Hello,

@StarManta : I’m pretty sure of where float speed is changed. But I’m going to investigate further more.

@psyydack : This is what I’m currently doing but it doesn’t correspond to my needs. The fact is I could set the variable through the inspector, which means that speed is not defined in the script.

My concern is that the var speedX is updated with the new value of speed at the end of the Update() (at each frame).

The only available way I could use is to set speedX outside the Update() method, there it won’t change.