Search Unity

c# giving me problems whit the && Operators

Discussion in 'Scripting' started by angelo813, Jul 6, 2019.

  1. angelo813

    angelo813

    Joined:
    Jul 1, 2019
    Posts:
    4
    not sure if this was asked i searched and couldn't really find anything and i am getting bad head aches from this

    so im just leaning c# and im trying to make a simple test script using && something like this

    void Update()
    {
    if (scores > 0 && lives >0)
    {
    Debug.Log("both scores and lives are above 0");

    }
    else {
    Debug.Log("both scores and lives are 0");
    }
    }

    but no matter what i do if i just change only one of the values of scores or lives it debugs that both are 0

    so only 1 condition is met not both as if im using OR || i tried it whit bools and still the same outcome .. hop i explained this right thanks
     
  2. ArdaOzcan

    ArdaOzcan

    Joined:
    Dec 9, 2017
    Posts:
    7
    Thats because else is called when the if statment is not true and it is only true when they are both above 0.In order to do this you need to use else if(). Code would be this:
    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4. if (scores > 0 && lives >0)
    5. {
    6. Debug.Log("both scores and lives are above 0");
    7.  
    8. }
    9. else if (scores == 0 && lives ==0) {
    10. Debug.Log("both scores and lives are 0");
    11. }
    12. }
    Note that you wouldn't get any log if only one of them is above zero. If you want that too here is the code
    Code (CSharp):
    1. void Update()
    2. {
    3. if (scores > 0 && lives >0)
    4. {
    5. Debug.Log("both scores and lives are above 0");
    6. }
    7. else if (scores == 0 && lives ==0) {
    8. Debug.Log("both scores and lives are 0");
    9. }
    10. else if(scores>0 && lives == 0){
    11. Debug.Log("only score is above zero.  lives is 0")
    12. }
    13. else if(scores==0 && lives > 0){
    14. Debug.Log("only lives is above zero.  scores is 0")
    15. }
    16.  
    17. }
     
    Last edited: Jul 6, 2019
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Both conditions must be true in order for the
    if
    block to run. So yes, changing only one of the values to a number less than 1 will result in a false condition, causing the
    else
    block to run instead.
    Here's an example using a pseudo-logic table:
    • scores = 1, lives = 1
      • [scores > 0 ?] = true
      • [lives > 0 ?] = true
        • [scores > 0 AND lives > 0 ?] = true
    • scores = 0, lives = 1
      • [scores > 0 ?] = false
      • [lives > 0 ?] = true
        • [scores > 0 AND lives > 0 ?] = false
    • scores = 1, lives = 0
      • [scores > 0 ?] = true
      • [lives > 0 ?] = false
        • [scores > 0 AND lives > 0 ?] = false
    • scores = 0, lives = 0
      • [scores > 0 ?] = false
      • [lives > 0 ?] = false
        • [scores > 0 AND lives > 0 ?] = false
    The only true condition involves both numbers being greater than zero.
    The logic is working fine; your debug messages are just not following it.
     
    Last edited: Jul 6, 2019
  4. angelo813

    angelo813

    Joined:
    Jul 1, 2019
    Posts:
    4
    ohhh ok that's simple logic i cant believe i didn't see that thank yous so much