Home > Software engineering >  "Only assignment, call, increment, decrement, await, and new object expressions can be used as
"Only assignment, call, increment, decrement, await, and new object expressions can be used as

Time:09-13

I'm new to unity and am following a course. I ran across this error:

Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

Here is my code:

using UnityEngine;

public class PlayerCollision : MonoBehaviour
{   
    public PlayerMovement movement;

    void OnCollisionEnter (Collision collisionInfo)
    {   
        if (collisionInfo.collider.tag == "Obstacle") 
        {
            Debug.Log("We hit an obstacle!");
            movement.enabled = false;
            FindObjectOfType<GameManager>().EndGame;
        }
    }
}

Can someone clarify to me what this means?

CodePudding user response:

This is expression is not a statement. It cannot be on its own.

FindObjectOfType<GameManager>().EndGame

You're missing something here.

FindObjectOfType<GameManager>().EndGame() is probably correct.

Also you should make sure that FindObjectOfType actually returns a GameManager instance, not null befor you invoke any methods or you'll cause an exception.

CodePudding user response:

FindObjectOfType<GameManager>().EndGame;

Does your GameManager script have EndGame public variable or function? If that is function, you missed (). So you can write like below;

FindObjectOfType<GameManager>().EndGame();

If that is public variable, you must assign it to the variable like this;

variable = FindObjectOfType<GameManager>().EndGame;
  • Related