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

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.

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.

In your start function you could perhaps do something like this

function Start()

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


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