Null Reference

For some reason I’m getting the following errors:

NullReferenceException: Object reference not set to an instance of an object
MouseWorld.GetPosition () (at Assets/Scripts/MouseWorld.cs:30)
Unit.Update () (at Assets/Scripts/Unit.cs:20)

From Scripts:

public static Vector3 GetPosition()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out RaycastHit raycastHit, float.MaxValue, instance.mousePlaneLayerMask);
return raycastHit.point;
}

and…

if (Input.GetMouseButtonDown(0))
{
Move(MouseWorld.GetPosition());
}

There are a couple of possiblities… either MouseWorld.instance is not set, or nothing was detected on the layer mask…

public static Vector3 GetPosition()
{
    if(instance==null) 
    {
         Debug.Log("No MouseWorld instance has been found.");
         return Vector3.zero;
    }
    if(Physics.Raycast(ray, out RaycastHit raycastHit, float MaxValue, instance.mousePlaneLayerMask)
    {
          return raycastHit.point;
    }
    Debug.Log("Nothing hit by MouseWorld.GetPosition()");
    return Vector3.zero;
}
1 Like

Thanks. Awake() was not capitalized. :man_facepalming:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms