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

Invalid expression term "&&", collision check

Discussion in 'Scripting' started by UnityForever777, Feb 2, 2020.

  1. UnityForever777

    UnityForever777

    Joined:
    Jan 15, 2018
    Posts:
    6
    I was writing a script for a platformer using a tutorial, where the code will keep checking whether or not the player is colliding with the ground. If the player is on the ground, the player can jump. Otherwise, the player cannot jump.

    I try running my code, and I get this error.

    "Invalid expression tern, '&&'"
    I've already tried reducing and increasing the number of &s, but it still does not work.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. Here is code:
    6.  
    7. [B]move.cs[/B]
    8.  
    9. public class move : MonoBehaviour
    10. {
    11.     public float moveSpeed = 100f;
    12.     public bool isGrounded = false;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.        
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         Jump();
    23.         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
    24.         transform.position += movement * Time.deltaTime;
    25.     }
    26.  
    27.     void Jump()
    28.     {
    29.         if (Input.GetButtonDown("Jump")) && isGrounded {
    30.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 10f), ForceMode2D.Impulse);
    31.         }
    32.     }
    33. }
    grounded.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class grounded : MonoBehaviour
    6. {
    7.     GameObject large;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         large = gameObject.transform.parent.gameObject;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.        
    18.     }
    19.  
    20.     private void OnCollisionEnter2D(Collision2D collision)
    21.     {
    22.         if (collision.collider.tag == "Ground")
    23.         {
    24.             large.GetComponent<move>().isGrounded = true;
    25.         }
    26.     }
    27.  
    28.     private void OnCollisionExit2D(Collision2D collision)
    29.     {
    30.         if (collision.collider.tag == "Ground")
    31.         {
    32.             large.GetComponent<move>().isGrounded = false;
    33.         }
    34.     }
    35. }
    36.  
     
  2. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    937
    In the first snippet, inside of your jump method, you have this line:

    Code (CSharp):
    1. if (Input.GetButtonDown("Jump")) && isGrounded {
    Which should be

    Code (CSharp):
    1. if (Input.GetButtonDown("Jump") && isGrounded) {
    Such that the if statement is properly closed.
     
    Last edited: Feb 3, 2020
    diXime and Joe-Censored like this.
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    For future reference: when asking for help with an error, it's very helpful if you can point out the exact line where the error occurs.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Even better, just copy and paste the error rather than trying to retype it. (the "tern" typo is a dead giveaway) Errors contain lots of information, and a lot of it is subtle unless you know what you're looking for; best to not risk losing any information.
     
  5. UnityForever777

    UnityForever777

    Joined:
    Jan 15, 2018
    Posts:
    6
    @Vanamerax Thank you, solved my problem. Sorry to have overlooked this simplistic solution.

    @Antistone and @StarManta My apologies, I will do that next time.
     
  6. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    937
    No problem, we've all been there :)