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 /
avatar image
0
Question by Harko33 · May 19, 2012 at 07:13 AM · prefabnullreferenceexceptionjoystick

NullReferenceException when using Single Joystick prefab

Hi everybody

I have the following problem with the Single Joystick prefab (in Standard Assets Mobile)

  • I drop the Single Joystick into my scene

  • Parameters : Dead Zone x = 0, y = 0.5, Normalized = checked

I want to read its x position. So here is what I wrote in my script :

 var speedRotation = 3.0;
 
 function Update()
 
 {
 
     var joy:Joystick = GetComponent(Joystick);
 
     transform.Rotate(0, joy.position.x * speedRotation, 0); // this is the line in error in the console
 
 }

When I launch my game, the console shows me the following error for this line :

NullReferenceException: Object reference not set to an instance of an object

It seems like the "joy" variable is null. So am I doing something wrong ? I tried to put the GetComponent(Joystick) in the Start() function, but same problem.

Thanks in advance

Comment
Add comment · Show 5
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 syclamoth · May 19, 2012 at 07:14 AM 0
Share

Do you have a 'Joystick' component on your object?

avatar image Harko33 · May 19, 2012 at 07:18 AM 0
Share

No, I just dropped the prefab into my scene. This prefab is not attached to any object, it is global. What is attached to my object is only this script. I tried to insert the prefab as a "son" of my object in the hierarchy, but I have the same problem.

avatar image syclamoth · May 19, 2012 at 07:20 AM 1
Share

An instantiated prefab is never 'global'. I'm not sure we're talking about the same thing here. What components does the object in question contain? You need to have a component of type 'Joystick' for that line to work.

avatar image Harko33 · May 19, 2012 at 07:43 AM 0
Share

This object has 3 components : - Transformation - GUITexture - Joystick

In my scene, I have created a cube and I want to control it with this joystick. So I dropped the prefab "Single Joystick" into my scene, and I dropped the script above on the cube.

Edit: maybe I should call GameObject.Find() before GetComponent(), to get a handler to my prefab ?

avatar image syclamoth · May 19, 2012 at 08:31 AM 0
Share

Yeah, you need to get the right object first.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Harko33 · May 19, 2012 at 03:37 PM

 var joy:Joystick = GameObject.Find("Single Joystick").GetComponent(Joystick);

did the trick ! Thanks to sysclamoth for inspiring me :)

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Null reference for variables(prefabs) when referencing another script 0 Answers

Bug moving a character with onscreen Joystick prefab 0 Answers

Possible Bug in Unity 5.4.1? NullReferenceException in console but everything works as expected (no old logs) 0 Answers

Help how to reference prefab for instantiating between loads. 0 Answers

GameObject.Find/WithTag returns null 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