Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 03, 2014 at 01:40 PM by ado112 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ado112 · May 05, 2014 at 05:49 PM · mobilevariablesjoystick

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.
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wibble82 · May 05, 2014 at 07:03 PM 0
Share

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.

avatar image ado112 · May 06, 2014 at 05:07 PM 0
Share

thanks,works fine now.

avatar image
2

Answer by xortrox · May 05, 2014 at 05:51 PM

My guess is that nothing is assigned to joystick in the editor.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ado112 · May 05, 2014 at 05:51 PM 0
Share

joystick is assinged.

avatar image ado112 · May 05, 2014 at 05:53 PM 0
Share

it says that the problem is on the row where the var V1 is assinged.

avatar image xortrox · May 05, 2014 at 05:58 PM 0
Share

If that's the case then you might fix it by assigning V1 in the Start function ins$$anonymous$$d.

avatar image Bunny83 · May 05, 2014 at 06:05 PM 0
Share

@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!");
avatar image ado112 · May 05, 2014 at 06:10 PM 0
Share

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.

Show more comments
avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ado112 · May 05, 2014 at 05:56 PM 0
Share

joystick has its value

avatar image wibble82 · May 05, 2014 at 06:12 PM 0
Share

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

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges