- Home /
null reference exception ... help!
nullreferenceexception object reference not set to an instance of an object
please help im a beginner and my head is about to explote by this i dont see the trouble in the code
var SplashObject : GameObject;
var UseRotation: boolean = false;
function Update () {
if(Input.GetButtonDown("Fire1")){
var hit : RaycastHit;
if (Physics.Raycast(Camera.main.transform.position,Camera.main.transform.foward, hit, 1000)){
var pos : Vector3 = hit.point;
var rot : Quaternion = Quaternion.identity;
if( UseRotation == true)
rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
Instantiate(SplashObject,pos,rot);
}
}
}
Answer by Fornoreason1000 · May 01, 2013 at 05:25 PM
line number 9, on the raycast I gave it test, and the ray casting is giving him problems. Camera.main.transfom.foward is returning null becuase he spelled forward wrong. lol
here the working code:
var SplashObject : GameObject;
var UseRotation: boolean = false;
function Update () {
if(Input.GetButtonDown("Fire1")){
var hit : RaycastHit;
if (Physics.Raycast(Camera.main.transform.position,Camera.main.transform.forward, hit, 1000)){
var pos : Vector3 = hit.point;
var rot : Quaternion = Quaternion.identity;
if( UseRotation == true)
rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
Instantiate(SplashObject,pos,rot);
}
}
}
null references are the most common errors I've ever seen, especially here in Unity. null references mean your aren't referencing something or something your referecing is null. best way to fix them is to check what vars are null using if statements and print("this var is null") also DaveA has a point , you did't really give enough info which can infuriate people. which is probably he deleted my half written answer lol.
anyways hope this helps
http://www.dotnetperls.com/nullreferenceexception
http://stackoverflow.com/questions/8645734/null-reference-exception-error-unity-c
yes it worked thanks a lot !!! but i still dont see the diference between your code and the $$anonymous$$e XD
That should have been a comment not an answer.
@ulisses154 I said what was wrong, you spelled forward wrong.
Answer by DaveA · May 01, 2013 at 04:59 PM
Without more info, I'm betting you never assigned SpalshObject in the Inspector