- Home /
Null Reference exception error
Hey guys,i recently started using unity for a game project that i have been planning.I have been following the tornado twins tutorials in youtube to learn most of the enviroment , i encountered the Null Reference exception problem and i really have no idea how to fix it since i don't know javascript that well.
The script basically makes a turret from firing bursts of many fireballs down to one fireball every two seconds,though even after i did that the video said i get loads of fireballs instead of one,here is the script.The errors that i get are from the compiler are from the shoot function,var bullit and the line below.Can anyone lend me a hand?
var LookAtTarget:Transform;
var damp = 6.0;
var bullitPrefab:Transform;
var savedTime=0;
var spawnPoint:Transform;
function Update ()
{
if(LookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if(oddeven)
{
Shoot(seconds);
}
}
}
function Shoot(seconds)
{
if(seconds!=savedTime)
{
var bullit = Instantiate( bullitPrefab, transform.Find("spawnPoint").transform.position , Quaternion.identity);
bullit.rigidbody.AddForce(transform.foward * 1000);
savedTime=seconds;
}
}
Answer by fafase · Apr 19, 2012 at 11:05 AM
Mmm bullit.rigidbody.AddForce(transform.foward * 1000); "forward" can you check if that goes better?
But the bullet Prefab is already in the inspector,i simply added the item that it should throw,in this case a fireball..
Did you change what I told you about the forward. You said "The errors that i get are from the compiler are from the shoot function,var bullit and the line below" and right below is this misspelling of forward.
It was exactly that!!It fires as it should now,once every 3 seconds!Though i still get a null ref exception at the 46th line,right above.
Yup that fixed the problem in that line!!!It finally shoots one bullet every 3 seconds!!!I still get like 50 errors at the 46th line (the one directly above),though it appears not to cause any trouble..should i be worried?
Warning can be ignored, errors are errors and might get back to you at some point. Try to isolate the origin. Try to modified the parameters of Instantiate one by one and see, if for example you use Vector3(0,0,0) ins$$anonymous$$d of the spawnPoint transform and the errors disappear, then you found it.
But, one clue could be that you error is co$$anonymous$$g in the update since you get it many times a second and shoot() is only accessed once every 2 sec. It could be that seconds or oddeven are doing wrong somehow.
At the moment, I cannot find anything wrong...
I'll look more into it,thanks for helping me out again!I started using Unity like five days ago and everything is still fairly new to me.
Answer by Wolfram · Apr 20, 2012 at 11:16 AM
Shoot() is called several times per second, since you cast Time.time to an integer, so for any time between 1.0000 and 1.9999, seconds will be 1, and Shoot() will be called. Normally, your usage of "savedTime" would prevent several bullets being spawned, but if you get a NullReferenceException, any code after that error is never executed, so "savedTime" is never updated.
Note that Find("spawnPoint") will search for an object named "spawnPoint" in your scene hierarchy - it has nothing to do with your variable "spawnPoint". So make sure you have a GameObject named "spawnPoint" in your scene. Or remove the transform.Find("spawnPoint").transform.position and simply replace it with spawnPoint.position, which will refer to your variable.
Your answer
Follow this Question
Related Questions
NullReferenceException Error 6 Answers
Instantiate() as GameObject = null reference 1 Answer
Why is this code getting NullReferenceException 2 Answers
Null Reference Exception error 3 Answers