[SOLVED] GameObject.Find doesn't work

Hey guys,

I’m hoping you can help me with this problem. I’ve been writing the code for our game for the last 2 months, and in that time, I’ve completely avoided using GameObject.Find, because I’ve never been able to make it work. I have tried every syntax derivation I can think of, and still, it always returns Null.

The funny thing is, we copied a project from my coworker’s computer, and the .Find code that was working there does NOT work on mine, even though the actual objects in the hierarchy, and the code was the same!

Can anyone help shed some light on this? Am I doing something catastrophically wrong?

Here’s the code I’m wrestling with. CoreRoot2 is the name of a game object in the hierarchy. gameLogic is the name of a script attached to that object. I’m using Unity 1.5, but had this same problem on the previous version.

var temp = GameObject.Find("CoreRoot2");
var script = temp.GetComponent(gameLogic) as gameLogic;

All I intend to do is access a public function from the gameLogic script, that’s it. Nothing crazy.

I appreciate any help you can give me! :slight_smile:

Wow, I knew all I’d have to do is post on the forum and it would magically start working. Isn’t that always the way?

If anyone else is stuck on this n00b problem:

var temp : GameObject; 
temp = GameObject.Find("CoreRoot2");

Gets the object just fine. Unfortunately, I still can’t get the script, I get “not a member of UnityEngine.Component.”

Anyone?

Just a question, is the game object deactivated? The Find will not find objects that are not enabled.

Just looking at it though, it looks kinda fine :confused:

I also make a habit of explicitly specifying where in the hierachy it is, for example

var awesomeObject : GameObject;
awesomeObject = GameObject.Find("/AwesomeStuff/AwesomeObject");

… This might not solve anything, but might be food for thought.

8 Likes

By the way, 12 thumbs up for a dune reference.

I would start to recommend that you declare your variables.

Automagic does not work with JS on the iphone.

var temp : GameObject = GameObject.Find("CoreRoot2");
var someScript : gameLogic = temp.GetComponent(gameLogic) as gameLogic;
someScript.ThisIsSomeMagicPublicFunction();

Thanks a lot, JTBentley,

Actually, I’ve got it finding the gameobject now, but I can’t seem to get the script. This is what I’m messing with at the moment:

var other : gameLogic = temp.GetComponent(gameLogic);

It seems like it should work, but I get “object reference not set to an instance of an object”… The game object and the script are both enabled btw :slight_smile:

Fellow Dune fan, eh? Awesome… I’ve read it so many times, what a great book! You ever seen the 4 hour director’s cut of the old movie?

both steps are required. Correct variable type + casting.

No cast = no gameLogic object :slight_smile:
No correct variable type = potentially no gameLogic object

Cast + Correct Variable type = either null or correct gameLogic object

Thank you dreamora, I appreciate your help!

I tried your code, but it still returns null for the script… any clues?

dreamora:
I’ve got it catching the gameobject, but strangely, if I put it into one line:

var temp : GameObject = GameObject.Find("CoreRoot2");

it says I’m not allowed to call this function when declaring a variable. It’s working the other way, but I don’t understand why this is…

if it returns null then the GameObject stored in temp has no component gameLogic

Perhaps its GameLogic, gamelogic or something else. (not 100% sure if JS on the iphone is case sensitive there)

The object does not exist? :slight_smile:

Btw, just reiterating the hierachy there… Without a / at the front (searching from root), the object will only search underneath the object’s parent.

Blah

  • Blah Child 1
  • Blah Child 2
  • Blah Child 3

If Blah Child searches, it will only search in everything underneath Blah, unless you specify otherwise.

gameLogic definitely exists, other scripts are calling static variables from it… Regarding the hierarchy, do I need to call the parent to find the script?

My hierarchy is:

CoreRoot2 (with attached gameLogic script)
– Child 1 (with the current script I’m calling from)

Could it not be working because the script I need is on the parent? I already have CoreRoot2 being called in a variable.

Oh, well if the gameobject is being found, err… gameObject.Find(“somescript”).Component maybe?

Drawing a blank :slight_smile:

I just tried swapping the two around so that Child1 is the parent of CoreRoot2, but that didn’t make any difference.

I appreciate your help though guys, thanks for such quick replies.

For clarity, this is exactly what I’m working with here:

var temp : GameObject; 
temp = GameObject.Find("CoreRoot2");

var someScript : gameLogic = temp.GetComponent(gameLogic) as gameLogic;

if (temp == null) {
    print("No gameobject found with that name!");
  } else {
  	print("gameobject: " + temp.name);
    if (someScript == null) {
	    print("No object found with that name!");
	} else {
	    print("someScript name: " + someScript.name);
	    //someScript.test();
	}
}

temp returns CoreRoot2, someScript returns null.

You need to ask the object that has the component for the component.
if its the parent, don’t use find at all, use transform.parent.gameObject instead.

Also, is the case right?
Sure the class isn’t called GameLogic? (thats normal naming convention, gameLogic would be a variable name)

Thanks dreamora,

The script is called gameLogic, is this not a good naming convention for a javascript name?

I’m sorry for my n00bness, but would you please help me with the code for using transform.parent.gameObject? I don’t quite get it. Can I call it as a variable the same way?

Oh man!!! I got it!! Yahooooooo :slight_smile: Been working on this for days!

Gentlemen, thanks so much for your help. It’s really cool of you to jump into my problem like this. I hope I can get good enough to pay it forward one of these days.

For anyone else with the same problem, here’s the code that finally worked (thanks to dreamora):

var temp : GameObject; 
temp = GameObject.Find("CoreRoot2");

someScript = transform.parent.gameObject.GetComponent(gameLogic) as gameLogic;

someScript.test();

In fact, all I needed was this:

someScript = transform.parent.gameObject.GetComponent(gameLogic) as gameLogic;
someScript.test();

sounds like you have twice the same object in then so it has actually retrieved the wrong one (the one without the component)