- Home /
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
}
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
}
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.
THAN$$anonymous$$ YOU SO $$anonymous$$UCH. Finally, something works :D I would upvote if I could
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.
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.
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?
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.
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?
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
I used
public var joystick : GameObject = GameObject.FindGameObjectsWithTag("JoystickObj");
but I'm getting
'Cannot convert 'UnityEngine.GameObject[]' to 'UnityEngine.GameObject'.'
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.
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
![](https://koobas.hobune.stream/wayback/20220613132305im_/https://answers.unity.com/themes/thub/images/avi.jpg)