NullReferenceException: Object reference not set to an instance of an object - Editor is OK, Build not working

Hi,
I have a problem with this error in title. I have two GameObjects: Plane and Dot. Plane has attached DotsScript and Dot has attached Click script like a component. Both writed in C#.

I want call method, which is in DotsScript from Click script. So (like in the tutorial) I have public GameObject plane and in editor I attached Plane gameobject. In code I try this (again, exactly like in tutorial):

public class Click : MonoBehaviour {
	public Vector3 startPoint;
	public GameObject plane;
	private DotsScript ds;
	
	void Awake () {
		ds = plane.GetComponent<DotsScript>();
	}

	void OnMouseDown ()
	{
		startPoint = gameObject.transform.position;
		ds.SetClickPoint(startPoint);
	}

}

In Editor everything works like a charm. When I build project on Android or on Windows I get this Error in output_log.txt:

NullReferenceException: Object reference not set to an instance of an object
  at DotsScript.SetClickPoint (Vector3 point) [0x00000] in <filename unknown>:0 
  at Click.OnMouseDown () [0x00000] in <filename unknown>:0 
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
UnityEngine.HitInfo:SendMessage(String)
UnityEngine.SendMouseEvents:SendEvents(Int32, HitInfo)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)

(Filename:  Line: -1)

Can you help me, please?

It looks like the error is in DotsScript not in Click. You might edit your question and add the DotsScript code.

1 Answer

1

Another thing: there are many things in your Assets folder that aren’t compiled into your game if you don’t have any references to them. So if you’re using, say, Shader.Find, and you don’t have any references to the shader you’re trying to find, it won’t be included and you’ll get an error.

Oh, Julien.Lynge you help me very much. I use exactly Shader.Find, which generates error too, but with this error was build fuctional until I start using references between DotsScript and Click.