Search Unity

Help required, error CS0131

Discussion in 'Editor & General Support' started by Deleted User, Apr 2, 2021.

  1. Deleted User

    Deleted User

    Guest

    Hi I am having some issues with the unity editor.
    Im trying to make a 2d platformer and when I added jumping I got the error "CS0131"
    Could someone tell me where the issue is?
    again....PNG

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour
    6. {
    7.     public float moveSpeed = 10f;
    8.     [SerializeField] private bool isGrounded = false;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     private void OnTriggerEnter2D(Collider2D other)
    17.     {
    18.         if (other.CompareTag("Ground"))
    19.             isGrounded = true;
    20.     }
    21.  
    22.     private void OnTriggerExit2D(Collider2D other)
    23.     {
    24.         if (other.CompareTag("Ground"))
    25.             isGrounded = false;
    26.     }
    27.  
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         if (Input.GetKey(KeyCode.LeftArrow))
    33.             transform.Translate(new Vector3(-2, 0, 0) * moveSpeed * Time.deltaTime);
    34.  
    35.         if (Input.GetKey(KeyCode.RightArrow))
    36.             transform.Translate(new Vector3(2, 0, 0) * moveSpeed * Time.deltaTime);
    37.  
    38.         if (Input.GetKeyDown("space") && isGrounded = true)
    39.         {
    40.             GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
    41.         }
    42.  
    43.     }
    44. }
    45.  
     
  2. Jihaysse

    Jihaysse

    Joined:
    Mar 29, 2020
    Posts:
    53
    Line 38:
    if (Input.GetKeyDown("space") && isGrounded = true)

    isGrounded == true

    it needs 2 = as one = is for asigning a value.
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Just to further explain, in C# a single = basically means "assign to the left side whatever is on the right side". A double == means "are the two sides equal, true or false?" When your line starts with "if" you use double ==.
     
  4. Deleted User

    Deleted User

    Guest

    Thank you both!!
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Remember: NOBODY memorizes error codes. The code is the least useful part of the error.

    The important part is the description of the error itself, the file it occurred in, and the line number and character position.

    All of that information is in the actual error message and you must pay attention to it.

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855