- Home /
NullReferenceExeption
Hi, I'm new to unity and programming and I cant find out what this error means: NullReferanceExeption: Object reference not set to an instance of an object Boo.Lang.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory.factory)
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name) PlayerMovementScript.OnTriggerStay (UnityEngine.Collider hitTrigger) (at Assets/Scripts/PlayerMovementScript.js:103) Here is the line of code the error is on: if (current.currentExtraAmmo < current.maxExtraAmmo) Please Help Me.
Just one thing - until you have 15 $$anonymous$$arma your questions are subject to moderation - please don't keep posting the same question as not all moderating users are able to delete the duplicates. I have deleted the copies on this occasion.
current isn't initialized to a value or it has been deleted. Can you post the code that sets up current (by editing your question and indenting it 4 spaces or 1 tab before pasting, or selecting it and clicking the code button after pasting).
Answer by The_Magical_Kiwi · Jul 11, 2012 at 11:11 AM
You get a null reference exception when your script tries to access a piece of information that isn't set to anything (it is null).
I'm assuming you know what a variable is? Variables normally have default values, for instance:
//Some pseudo code
int foo;
print(foo);
This should print "0" because an integer is 0 by default.
References are a pointer to an object. For instance if you have a 'Bike' class, you can create a reference to it in the same way you declare a variable.
//something like (again pseudo code)
int speed = 10;
//This next line creates the reference 'myBike'.
//myBike is a reference to the new instance of the Bike we are creating.
Bike myBike = bike(speed);
For simplicity sake, it isn't too important right now that you know the difference between a reference and a variable, what is important to know is that references don't have default values. If you don't set it to something it remains a reference to nothing, it is null.
If you try to look at or interact with a null reference you will get the error "null reference exception".
if (current.currentExtraAmmo < current.maxExtraAmmo)
In your case, it would appear that the reference 'current' is null, it hasn't been pointed to an instance of anything. It can't check if extraAmmo is less than maxExtraAmmo because it doesn't know what object to look in.
A very long winded answer I know, but I hope it explains why you got the error and not just how to fix it without you understanding the cause.
Also it is extremely difficult to answer this question without using some simple programming jargon such as object, variable, pseudo code, reference and so on. However a quick google search should be able to explain these concepts much better than I can.
Hope this has helped.
Your answer
Follow this Question
Related Questions
I am having a problem with the following script 1 Answer
A node in a childnode? 1 Answer
NullReferenceException 1 Answer
Change Automatic to Semi-Automatic 1 Answer
Camera's wont change 2 Answers