Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Variable value inside Update()

Discussion in 'Scripting' started by Seeane, Apr 9, 2015.

  1. Seeane

    Seeane

    Joined:
    Jan 14, 2014
    Posts:
    2
    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.

    Any advice?

    Code (CSharp):
    1.  void Update()
    2.     {
    3.         GameObject model = GameObject.Find("Model");
    4.         float speedX = speed;
    5.  
    6.         //Get inputs    
    7.         float moveH = Input.GetAxis("Horizontal");
    8.         float moveV = Input.GetAxis("Vertical");
    9.  
    10.         //Player Character grounded
    11.         if (Physics.Raycast(transform.position, Vector3.down, 1f))
    12.         {
    13.             //Move
    14.             transform.Translate(new Vector3(moveH, 0, moveV) * Time.deltaTime * speed);
    15.         }
    16.         //Player Character !grounded
    17.         else
    18.         {
    19.             //Move + Fall
    20.             transform.Translate(new Vector3(moveH, -1, moveV) * Time.deltaTime * speed);
    21.             if (model.transform.rotation.x < 0.50)
    22.             {
    23.                 model.transform.Rotate(new Vector3(120, 0, 0) * Time.deltaTime, Space.World);
    24.             }
    25.             else if (model.transform.rotation.x > 0.50)
    26.             {
    27.                 model.transform.Rotate(new Vector3(0, 0, 0));
    28.                 if (Input.GetKey(KeyCode.Space))
    29.                 {
    30.                     if (model.transform.rotation.x < 0.90)
    31.                     {
    32.                         model.transform.Rotate(new Vector3(120, 0, 0) * Time.deltaTime, Space.World);
    33.                         speed = 15;
    34.                     }
    35.                 }
    36.                 else
    37.                 {
    38.                     if (model.transform.rotation.x > 0.50)
    39.                     {
    40.                         model.transform.Rotate(new Vector3(-120, 0, 0) * Time.deltaTime, Space.World);
    41.                         speed = speedX;
    42.                     }
    43.                 }
    44.             }
    45.         }
    46.     }
     
    Last edited: Apr 9, 2015
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,745
    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)
     
  3. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    You need another variable to defaultSpeed;

    Code (CSharp):
    1.  
    2. float defaultSpeed = 15; //your first variable
    3. void Update()
    4.     {
    5.         GameObject model = GameObject.Find("Model");
    6.         float speedX = defaultSpeed;
    7.         //Get inputs  
    8.         float moveH = Input.GetAxis("Horizontal");
    9.         float moveV = Input.GetAxis("Vertical");
    10.         //Player Character grounded
    11.         if (Physics.Raycast(transform.position, Vector3.down, 1f))
    12.         {
    13.             //Move
    14.             transform.Translate(new Vector3(moveH, 0, moveV) * Time.deltaTime * speed);
    15.         }
    16.         //Player Character !grounded
    17.         else
    18.         {
    19.             //Move + Fall
    20.             transform.Translate(new Vector3(moveH, -1, moveV) * Time.deltaTime * speed);
    21.             if (model.transform.rotation.x < 0.50)
    22.             {
    23.                 model.transform.Rotate(new Vector3(120, 0, 0) * Time.deltaTime, Space.World);
    24.             }
    25.             else if (model.transform.rotation.x > 0.50)
    26.             {
    27.                 model.transform.Rotate(new Vector3(0, 0, 0));
    28.                 if (Input.GetKey(KeyCode.Space))
    29.                 {
    30.                     if (model.transform.rotation.x < 0.90)
    31.                     {
    32.                         model.transform.Rotate(new Vector3(120, 0, 0) * Time.deltaTime, Space.World);
    33.                         speed = 15;
    34.                     }
    35.                 }
    36.                 else
    37.                 {
    38.                     if (model.transform.rotation.x > 0.50)
    39.                     {
    40.                         model.transform.Rotate(new Vector3(-120, 0, 0) * Time.deltaTime, Space.World);
    41.                         speed = speedX;
    42.                     }
    43.                 }
    44.             }
    45.         }
    46.     }
     
  4. Seeane

    Seeane

    Joined:
    Jan 14, 2014
    Posts:
    2
    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.