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 Domain · Feb 02, 2014 at 12:07 AM · javascriptjoystick

Drag-and-Drop to Access a GameObject not working!

I have a script attached to my player, I need it to access the script of the Joystick prefab I have in the scene. I have had no luck using GameObject.Find() or any of the other ways to access it through code, so I'm trying to get it to work through dragging the joystick gameObject in the editor into its slot. However, it's not working! I get no errors, but when I run the game, I get "Object reference not set to an instance of an object". Even though I have already dragged and dropped it. This is working for another script, and there is no difference between this one and the other one. GAH THIS IS FRUSTRATING.

 public var joystick : Joystick;
 
 function Update () { 
     if (joystick.position.x > .15 || joystick.position.x < -.15) {
           //sample code
     }
Comment
Add comment · Show 3
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 GTX Titan · Feb 02, 2014 at 11:10 AM 0
Share

You could try this:

 private var joystick:Joystick; //change Joystick to your scripts name that holds the variable

 function Start(){

 joystick = GetComponent(Joystick);

 }

then you can use your if statement as it is:

 if (joystick.position.x > .15 || joystick.position.x < -.15) {

 //sample code

 }
avatar image Domain GTX Titan · Feb 02, 2014 at 06:56 PM 0
Share

I'm having prolems with the 'var joystick: ScriptName'. Whenever I try to do that, it gives me an error saying ScriptName does not denote a valid type (even when I have ScriptName located with GetComponent or the other ways find it). The code in the unity documentation has, basically, not worked. It's giving me errors.

avatar image Domain GTX Titan · Feb 02, 2014 at 11:29 PM 0
Share

THAN$$anonymous$$ YOU SO $$anonymous$$UCH. Finally, something works :D I would upvote if I could

3 Replies

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

Answer by GTX Titan · Feb 02, 2014 at 08:10 PM

If your script is named JoystickScript or something just put in private var joystick:JoystickScript; I use it the same way... works if the script is on the same game object... so maybe you should do this:

 private var joystickObj:GameObject;
 
 private var joystick:Joystick; // again replace Joystick with the script file's name
 
 function Start(){
 
 joystickObj = GameObject.Find("Your gameobject's name with the joystick script");
 
 joystick = joystickObj.GetComponent(Joystick);
 
 }

Then you can use the variable I think.

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
avatar image
0

Answer by morgan23 · Feb 02, 2014 at 12:56 AM

hmm I had the same problem is it not showing up when you press play? try making a game object variable and drag it then try joystick = gameobject.getcomponet see if that works. I used this too get it too work.

Comment
Add comment · Show 1 · 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 Domain · Feb 02, 2014 at 01:37 AM 0
Share

I need to access a variable that's attached to the script that's attached to the joystick GameObject. How would I go about doing that?

avatar image
0

Answer by markmmiller · Feb 02, 2014 at 12:28 AM

In your start function you could perhaps do something like this

 function Start()
 {
    this.AddComponent("Joystick");
 }

Not sure if it's what you are looking for but your problem seems to be you haven't correctly attached a joystick script to your player.

If you still want to find the object whilst it is in your scene you could perhaps do something like this instead

 GameObject.FindGameObjectsWithTag("JoystickObj");

Which should work as long as the object in your scene is tagged with the specific tag.

Comment
Add comment · Show 5 · 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 Domain · Feb 02, 2014 at 01:38 AM 0
Share

I need to access a variable that's attached to the script that's attached to the joystick GameObject. I think I could use this to access the object, but then what?

avatar image markmmiller · Feb 02, 2014 at 11:01 AM 0
Share

Inside your joystick script make sure that the variable you are trying to access is public or has the appropriate get/set methods.

Finally to get the actual property on the script you can do something like this.

 // Once you know the object which has it attached do this
 Joystick joy = obj.GetComponent("JoystickObj");
 
 // Then using dot notation do something like
 float speed = joy.speed; 
 
 //or whatever value it is you're looking for
avatar image Domain · Feb 02, 2014 at 07:03 PM 0
Share

I used

 public var joystick : GameObject = GameObject.FindGameObjectsWithTag("JoystickObj");

but I'm getting

'Cannot convert 'UnityEngine.GameObject[]' to 'UnityEngine.GameObject'.'

avatar image markmmiller · Feb 02, 2014 at 07:13 PM 0
Share

That function returns an array of values which refer to ALL objects in the scene with that tag, if you want the first one you will have to change it to.

 public var joystick : GameObject = GameObject.FindGameObjectsWithTag("JoystickObj")[0];

Also you need to make sure the object with the script attached has that as it's tag as well.

avatar image Domain · Feb 02, 2014 at 11:29 PM 0
Share

Thank you for your help! I'm sure this would have worked (I just tried the other answer's suggestion first). Thanks to all of you who took the time to help me out

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

21 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

Related Questions

How to change the virtual joystick Input direction depending on camera rotation so the player moves to its current forward direction? 0 Answers

How to use the joystick.js in unity3d?? 0 Answers

Setting Scroll View Width GUILayout 1 Answer

I need help with my 2D Android character controller 0 Answers

[mobile development] 2D Joystick Controller 0 Answers


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