Weirdness w/ GameObject.Find... Bug?

Hiya,

I am looking up a GO which is constantly returning “null” though I know for a fact it is there. Here is the scenario…

I lookup a GO if it is not there then Instantiate one, name and tag it. Now when I perform this action a second time the GO is still there, I can see it in the heirarchy but Find keeps returning “null”. The Find code looks like this:

var charGO : GameObject = GameObject.Find(characterInfo[c].CharacterName());

I have also tried some variations of it just like this (as I noticed with some of the GetComponent/AddComponent stuff you need quotes around your stringed variables but had the same results… That attempt looked like this:

var charGO : GameObject = GameObject.Find("" + characterInfo[c].CharacterName() + "");

The reason I am feeling this may be a bug or a bad limitation is if I replace this: characterInfo[c].CharacterName()

with something like this (the actual name of the GO):

var charGO : GameObject = GameObject.Find("Mackenzie");

It works as expected, unfortunately I do not have the luxury of knowing/guaranteeing the name.

Any thoughts?

Regards,

– Clint

If you print the characterInfo.CharacterName() to the console (Debug.Log) just before calling Find - does it print the correct name?

Yes.

In fact I use the CharacterName() to set the name of the GO so I can see there that it is the same as well.

Thanks,

– Clint

try:

var charName : String = characterInfo[c].CharacterName();
var charGO : GameObject = GameObject.Find(charName);

I actually tried that as well…

In addition to:

var charName : String = characterInfo[c].CharacterName(); 
var charGO : GameObject = GameObject.Find("" + charName + "");

var charGO : GameObject = GameObject.Find(characterInfo[c].CharacterName().ToString());

var charGO : GameObject = GameObject.Find("" + characterInfo[c].CharacterName().ToString()+ "");

They all yield the same results of not finding the GO…

Thanks!

– Clint

Another thing you could try is parenting the instantiated GO to another GO that you always have a reference to, like MyCharacterParent, and then using MyCharacterParent.transform.FindChild(). If the transform is found, get the game object from it.

That is a good thought and could be a nice workaround. :slight_smile:

Thanks!

– Clint

Okay, apparently not a bug (whew… :-p) but I tracked it back to a type casting issue. Thanks to all for some suggestions on handling!

Regards,

– Clint