unity-game-engine

Unity: NullReferenceException: Object reference not set to an instance of an object


I am following an Unity tutorial. I face a problem when trying to detect collision in the game. This is the error:

NullReferenceException: Object reference not set to an instance of an object

This is the script:

using UnityEngine;

public class Collide : MonoBehaviour
{
    public Movement movement;     // A reference to our PlayerMovement script

    // This function runs when we hit another object.
    // We get information about the collision and call it "collisionInfo".
    void OnCollisionEnter(Collision collisionInfo)
    {
        // We check if the object we collided with has a tag called "Obstacle".
        if (collisionInfo.collider.tag == "Obstacle")
        {
            movement.enabled = false;   // Disable the players movement.
            Debug.Log("Coollision occured");
        }
    }
}


Solution

  • As i saw in the second image you have not added the movement reference to the movement field. At the same in the script also you are not assigning the reference. Try to assign at editor or you can create object.