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. Dismiss Notice

Error with If statement

Discussion in 'Scripting' started by thegillion, Feb 18, 2015.

  1. thegillion

    thegillion

    Joined:
    Jan 26, 2014
    Posts:
    5
    I am working on a game that when a mana level get to 0 you have to wait out a recharge or watch a ad. I have everything else working but I get a wired loop in my statement.

    if (PlayerPrefs.GetInt ("Mana_current") == 0) {
    Debug.Log("Before If " + Random_event.Run);
    if(Random_event.Run = true){
    Random_event.Run = false;
    Debug.Log("After If " + Random_event.Run);
    }

    Now it should only run Random_event.Run = false; once but I get this in my debug log.

     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    "if(Random_event.Run = true)" should be "if(Random_event.Run == true)"

    I has something like this myself some time ago as well, and it seems like, if there is an error in an if statement, it just gets ignored instead of an error being thrown. No idea why though.
     
    thegillion likes this.
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    A comparison uses two egual signs, you are only using one on the second if statement.
     
    jtsmith1287 likes this.
  4. thegillion

    thegillion

    Joined:
    Jan 26, 2014
    Posts:
    5
    Thanks Timelog that works but you are right I was looking for a error and it doesnt happen.
     
  5. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Nothing to do with whether or not it is in an if statement - It just isn't a compilation error. A single equal sign is a valid assignment operation.

    Also, when using booleans in an if, it is redundant to compare with '== true'

    Code (csharp):
    1.  bool myBool=true;
    2. if (myBool==true) //this is equivalent to the below statement
    3. if(myBool) //this is equivalent to the above statement
    4.  
     
  6. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    What I am wondering though, is WHY an assignment operation within the if statement is not a compilation error. What is the use of an assignment operation within a comparison operation? Personally I would expect C# to throw an error at that point.
     
    dterbeest likes this.
  7. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    I wonder about this too. I know that Pascal compilers give an "Expected type Boolean expression" when you try to do this
     
  8. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    https://msdn.microsoft.com/en-us/library/sbkb459w.aspx

    Essentially, an assignment operator has a return value - the value of the lefthand side variable after the completion of the statement. So the line

    Code (csharp):
    1. bool a = false;
    will return a value of false;

    Code (csharp):
    1. bool a=false;
    2. bool b;
    3. a=(b=true);
    4. Debug.Log(a);
    5.  
    The above should print True, because a was assigned the return of (b=true), which was 'true'.

    Therefore

    Code (csharp):
    1.  
    2. bool a=false;
    3. if (a=true) Debug.Log ("True");
    4. else Debug.Log ("False");
    5.  
    This code should print 'True', because the return of 'a=true' was true.

    If you try any NON-bool assignment operation inside an if statement, you will get a compile error.

    Code (csharp):
    1.  
    2. int a=0;
    3. if(a=1) Debug.Log ("A is 1");
    4.  
    This throws a compilation error because the return value of 'a=1' is the int 1. The error should be along the lines of "Cannot implicitly convert type 'int' to 'bool'. The error is because the return value of the assignment is an int, and you are attempting to use it as a bool.

    Good editor programs may point out boolean assignment operators inside of if statements, and maybe throw a warning, but they do not throw an error because it is all completely valid.
     
    jtsmith1287, Timelog and dterbeest like this.
  9. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    @Zaladur thanks for you explanation! Makes total sense
     
    Timelog likes this.
  10. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Thanks for the explanation, it makes a lot of sense indeed :)