- Home /
Mouse position giving errors.
I've got an empty game object that follows the mouse's position around the screen with a ray cast aiming towards the ground, for the first few frames of play it is hitting the ground but soon after I get this error continuously.
NullReferenceException: Object reference not set to an instance of an object
BuyObjectScript.Update () (at Assets/Scripts/BuyObjectScript.cs:26)
Line 26 is:
L26- if (hit.transform.tag == "Ground"){
BuyPreviewModel.isGrounded = true;
}
Well, the only object on that line is hit
. And the error is telling you that it's null.
'BuyPreview$$anonymous$$odel' is almost certainly null. Without knowing the type of 'BuyPreview$$anonymous$$odel', seeing the code around this statement, and/or seeing how BuyPreview$$anonymous$$odel is to be initialized, we cannot give you much else.
The if statement you have, "hit" is independant of the "BuyPreview$$anonymous$$odel" , so what you are doing is checking if the BuyPreview$$anonymous$$odel is hit, if yes, ground this. You need to add this just above:
if(BuyPreview$$anonymous$$odel)
if (hit.transform.tag == "Ground"){
BuyPreview$$anonymous$$odel.isGrounded = true;
}
The reason being that if you have not initialized or told Unity where "BuyPreview$$anonymous$$odel" object is, it will not know and it will try to run its code anyway.. and when null, you get the above error.
Hope that helps.
Your answer

Follow this Question
Related Questions
Raycast based on a Rect? 1 Answer
A node in a childnode? 1 Answer
Reacting to Mouse animation... 2 Answers
C# move main camra via mouse position on boarders around screen script for RTS Game 0 Answers