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 won't change value after if statement

Discussion in 'Scripting' started by Aapakaaq, Aug 20, 2016.

  1. Aapakaaq

    Aapakaaq

    Joined:
    Aug 16, 2016
    Posts:
    21
    Greetings everyone.

    I'm currently creating a 2D platformer, and the E button will rotate the player. Every time the player rotates clockwise it will add 1 to a variable, after 3 times the variable will be 3, and the player has moved 360 degrees. That should reset the variable and repeat this. But it won't increase the variable, after the 3rd, it is stuck at 0, the player is able to rotate

    Code (CSharp):
    1.         if (Input.GetKeyDown(KeyCode.E)) {
    2.             checkQR += 1;
    3.             transform.localEulerAngles -= new Vector3(0.0F, 0.0F, 90.0F);
    4.  
    5.             if (checkQR == 4) {
    6.                 QRReset();
    7.             }
    8.  
    9.             Debug.Log(checkQR);
    10.         }
    The QRReset:

    Code (CSharp):
    1.     void QRReset () {
    2.         checkQR =0;
    3.     }
     
    Last edited: Aug 21, 2016
  2. MagicZelda

    MagicZelda

    Joined:
    May 1, 2013
    Posts:
    90
    try Input.GetKeyUp instead of Input.GetKeyDown just an idea
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    You said you're hitting Q to rotate but your script is E. Typo or did you mix up something? Also, I assume this if statement is in your Update method?
     
  4. Aapakaaq

    Aapakaaq

    Joined:
    Aug 16, 2016
    Posts:
    21
    Thanks for your reply. I wish the code to run when you press the key down and not when you release it, but I did try it and the variable wont change
     
  5. Aapakaaq

    Aapakaaq

    Joined:
    Aug 16, 2016
    Posts:
    21
    Thanks for your reply. I did mean E, I'm sorry for the mistake. The if statement is in the update method.
     
  6. Aapakaaq

    Aapakaaq

    Joined:
    Aug 16, 2016
    Posts:
    21
    The problem has been solved, the console was on "collapse"... *Facepalm