- Home /
whats wrong? nullpointer
whats wrong with this code, in my other scene it works perfectly. im trying to use the same code in another script and scene. everytime i run it, it crash instantly.
NullReferenceException: Object reference not set to an instance of an object RaycasterBasic2.OnGUI () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/RaycasterBasic2.js:46)
here is my code:
if(hit.collider.gameObject.tag == "cat"){ //<---line 46
{
Timer += Time.deltaTime;
if (Timer > 5)
GUI.Box(Rect(Screen.width / 1.35 - 100, Screen.height / 2 - 100, 300, 150), "CAT", GuiStyle);
}
}
Answer by Radivarig · Dec 05, 2014 at 01:04 PM
place this above line 46
if(hit ==null) Debug.Log("hit is null");
if(hit.collider ==null) Debug.Log("hit's collider is null");
the one that debugs to console is causing the error. I assume your hit returned null because it didn't hit anything so it collided with nothing.
To prevent
if(hit && hit.collider && hit.collider.gameObject.tag == "cat"){
allright, so this one helps me to found which one occurs the nullointer. It was the "hit's collider is null". So how can I fix this? I have no idea....
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to save and display players times using Playerprefs in Javascript? 1 Answer
play audioclip problem 1 Answer
Accuracy doesn't work. No enemies showing up. 1 Answer
How to display previous players scores as well as current player score at the end of the game? 0 Answers