- Home /
finding instantiated prefabs
I have an object that has been instantiated like this:
Instantiate(Resources.Load("MyPrefab"));
Visually I can see that that is happening when I test the program. In another script, after this has been done, I'm trying to reference that instance, but this code returns null:
boss = GameObject.Find("MyPrefab");
Why? Shouldn't that return the first one currently in the scene? I guess GameObject.Find() doesn't find Instantiated objects? I looked for a "name" field in the prefab in case that was the source of the issue, but I'm not seeing one.
Look at the hierarchy when you instantiate an object...what do you see?
@fattie - Wow, interesting, thanks..
@eric - "$$anonymous$$yPrefab(Clone)" ? Really? Is that documented/reliable? Hmm, this code still says it's null, even though I see those showing up in the scene hierarchy tree:
var prefab : GameObject = GameObject.Find("$$anonymous$$yPrefab(Clone)") as GameObject;
Debug.Log("prefab is null: " + ((prefab == null) ? "true" : "false"));
I put in some test code that instantiates it and then looks for it, and it does find it, using that name. So, it's some kind of other problem with my code. But this is good to know, thanks again for the response.
If you don't want (Clone) added, you can rename it when you instantiate it.
Answer by ThePunisher · Oct 15, 2012 at 05:53 PM
Could it be that it's not the real name of the instance? Just trying to figure out if that's a possibility.
I would try renaming the GameObject you just instantiated to something else, then looking for that name.
Oh, another thing....are you sure your code which is looking for the GameObject is happening after the object has been Instantiated? I would use a debug statement to very the order.
If you are instantiating and looking in different Start/Awake methods it could be that the wrong Start/Awake is happening first.
I put in some test code that instantiates it, renames it, then looks for it under that name, and sure enough, it finds it. So it's like you're saying, there's some kind of an ordering issue in my code. I don't see how that's possible, because the code that is looking for it is kicked off by the code that first instantiates the prefab, but somehow it must be. Anyway, that's interesting, you can rename an object after instantiating it, then look for it by that name. That's good to know, thanks for the response.
Answer by Brenden-Frank · Nov 11, 2012 at 06:50 AM
This may be a little late and I know this was already answered, but you can very easily refer to your instantiated prefab by creating a variable and making it equal to the instantiate's return value:
GameObject myGameObject = Instantiate(Resources.Load("MyPrefab", typof(GameObject))) as GameObject;
myGameObject.name = "SomePrefabName";
You can then send it to your other script via a public variable or store it here with something vague...like
GameObject.Find("SomeOtherObject").GetComponent<SomeOtherScriptOnThatObject>().myOtherScriptGameObjectVar = myGameObject;
Bearing in mind of course that Find() and GetComponent() should be used as much as possible outside of frame loops.