- Home /
The question is answered, right answer was accepted
Why am i getting NullReferenceException?
Can anybody tell me why am i getting NullReferenceException because of:
//part of the script.
var joystick : Joystick; //using joystick from mobile assets.
var V1 : Vector2 = new Vector2(joystick.position.x,joystick.position.y); //float variables.
Answer by wibble82 · May 05, 2014 at 06:20 PM
I think I see why you might be getting the error. Correct me if I'm wrong but
you have assigned joystick a value by dragging an object/prefab into it in the editor
thus you are expecting it to be valid when that line of code (for V1) executes
however this isn't really what happens internally. Assuming your lines of code are declarations of member variables, the assignments are part of the object's constructor. i.e. they get executed the moment the object is allocated in memory.
Now in unity, when the object is actually created, it will go through a few steps:
first, your object is allocated
next, its constructor is called
next, the data setup in the property editor is 'deserialised' into it
finally, the Start function is called on it (if you've provided one)
So even though you might think you've assigned something to joystick, when that line of code is executed internally it hasn't been setup yet.
To do this kind of initialization, where you're dependent on values the user has specified in the property editor, add it into your start function. So you should declare the V1 variable as a member where you are doing, but don't assign it anything. Then add your:
V1 = new Vector2(joystick.position.x,joystick.position.y);
Inside the Start function.
The start function is guaranteed to be executed after unity has poked in all the property editor values so things will be setup correctly. As a rule, put any remotely complex initialization in here.
Hope that helps
-Chris
Ok, it should look more like this:
var P : Transform;
var joystick : Joystick;
private var V1 : Vector2;
private var V2 : Vector2;
function Start () {
V1 = new Vector2(joystick.position.x,joystick.position.y);
V2 = new Vector2(P.position.x,P.position.y);
}
The key here is that I have left your V1 and V2 variables DECLARED as normal, just like your P and joystick variables. I'm assu$$anonymous$$g you are setting P and joystick to reference something using the property editor.
However I am waiting until the Start function before INITIALISING V1 and V2. This is because they depend on P and joystick.
You can think of it this way. When unity first creates your object, it is 'blank'. None of its members have been set to anything at all. The next thing it does is call your constructor, which goes through and executes all the lines of code in front of your member variables. Then it looks at the values you've specified in the editor and pokes them in. And finally, it calls 'Start'.
The result of that behaviour is that you can't rely on anything specified in the editor (or indeed, anything from anywhere else in unity at all) until the Start function is called.
So simple things like "var myvalue = 10" are safe, because they don't rely on any other data. But more complex things that try and read the state of other game objects must wait until the start functions.
Answer by xortrox · May 05, 2014 at 05:51 PM
My guess is that nothing is assigned to joystick in the editor.
it says that the problem is on the row where the var V1 is assinged.
If that's the case then you might fix it by assigning V1 in the Start function ins$$anonymous$$d.
@ado112: If the error is really in that line, it has to be the joystick variable because it's the only reference that is involved here.
Try adding this Debug.Log before the second line:
if (joystick == null)
Debug.LogError("If you can read this the joystick variable is null!");
the reason because im asking this is because im trying to make my character to move with the single joystick and i need an angle from joystick origin to new position for rotation.
Answer by zee_ola05 · May 05, 2014 at 05:51 PM
Because you didn't set the value of joystick. You only set its type.
I agree with zee - there's no way joystick has its value. Unless joystick is some weird object who's position property is trying to access a null object, but I think its more likely you're just wrong about joystick being setup correctly. I can't actually see how joystick could have a value anyway
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
why is my character rotation always 180° after joystick is released. 1 Answer
Defining a variable in another script? 1 Answer
What is way to flash the screen WITHOUT using a Texture? 2 Answers
Public variable in script different for every game object. 1 Answer