. Advertisement .
..3..
. Advertisement .
..4..
Here is the program I run:
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");
}
}
}
After I run, it returns an error:
NullReferenceException: Object reference not set to an instance of an object
Does anyone have any suggestions for the problem below: unity nullreferenceexception: object reference not set to an instance of an object in the programs. How to correct it?
The cause:
You haven’t added the movement reference in the movement field, as i noticed in the second picture. The script does not assign the reference. Therefore, the error happens.
Solution:
You can resolve this error by assigning the reference at editor or creating an object.
You haven’t set the movement field for your Collide component. It is possible to add it via the Unity Editor, or you can include the following line into your Start function for Collide: