Search Unity

How to make an "and" function? variables?

Discussion in '2D' started by PinkyP17, Aug 5, 2019.

  1. PinkyP17

    PinkyP17

    Joined:
    Aug 2, 2019
    Posts:
    5
    So I just got into unity when I stumbled upon this problem.

    I followed Brackeys code tutorial on player movement when I realized that the player can jump as much as they want.

    So I tried to make a isgrounded boolean but I can never find the correct way to implement it into my codes.

    I want to make a code if (Input.GetButtonDown("Jump")) && isgrounded = true;
    {
    jump = true
    isgrounded = false
    }

    But every time I use the &&, it said that it's not possible or something along those lines. I know that it's the basic fundamental to know about this but I can't seem to figure it out.

    Any help would be appreciated and sorry for my lack of English vocabulary, I am still learning so excuse me if I make any mistakes.
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Note this is misunderstanding the C# language and not misunderstanding Unity.

    Three problems:
    • The expression should all be within the brackets so "if (SomeExpression)"
    • = is used to assign a value not compare equality. You should use "=="
    • ; (semi colon) at the end of that line isn't supposed to be there.
    So you should end up with:
    Code (CSharp):
    1.         if (Input.GetButtonDown("Jump") && isgrounded == true)
    2.         {
    3.             jump = true;
    4.             isgrounded = false;
    5.         }
    It's also a minor thing but you can implicitly check if something is true/not null by doing this subtle but sometimes easy-on-the-eyes code:
    Code (CSharp):
    1.         if (Input.GetButtonDown("Jump") && isgrounded)
    2.         {
    3.             jump = true;
    4.             isgrounded = false;
    5.         }
    6.  
     
    Last edited: Aug 5, 2019
    Antypodish and Deleted User like this.
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    When you use Code Tag it would be easier to see your code.

    You first IF statement linę is incorrect.
    You need put first condition && and second condition in same brackets. For example


    Code (CSharp):
    1. if ( conditionA >= 0 && isConditionBTrue )
    2. {
    3.   // some code
    4. }
    Edit:
    Sorry I writing from mobile.
     
    Last edited: Aug 5, 2019
    PinkyP17 likes this.
  4. PinkyP17

    PinkyP17

    Joined:
    Aug 2, 2019
    Posts:
    5
    Ty for the help, I really appreciate this!
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    You're welcome.
     
    PinkyP17 likes this.
  6. PinkyP17

    PinkyP17

    Joined:
    Aug 2, 2019
    Posts:
    5
    So I need both conditions in a same bracket like this

    if (Input.GetButtonDown("Jump") && isgrounded = false; )

    Or am I missing the point here?
    Sorry for this but I don't get the point your are telling me...

    Can you explain it a little more details?
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    I gave you an example of the correct code above, simply look at the difference if you don't follow my explanation. If I explain in more detail it seems it'll make things more confusing for you.
     
    PinkyP17 likes this.
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Melv explained you best way.
    You need this
    Code (CSharp):
    1. if (Input.GetButtonDown("Jump") && isGrounded == true)
    or its simplified form
    Code (CSharp):
    1. if (Input.GetButtonDown("Jump") && isGrounded )
    meaning
    isGrounded is same as isGrounded == true.
    Also, in comparison loops, you don't use normally single equal sign.
    This is wrong.
    use double equal sign for comparison
    Code (CSharp):
    1. isgrounded == false
    Singe equal sign is for assignment, like a = 2.
    Also, you don't put semicolon inside if loops. This is wrong ( see ; character after false ).
    Corrected with if statement is as following
    Code (CSharp):
    1. if (Input.GetButtonDown("Jump") && isgrounded == false )
     
    PinkyP17 and MelvMay like this.