- Home /
How do I instantiate a game object from a string variable?
So I have no problem instantiating Objects normally until I came across this today:
I want to instantiate a game object from its saved model sanem:String, but I get the error cant convert STring to Object.
var h:GameObject=Instantiate(gameplaySceneManagerScript.clueNodeArray[x].model.name);
How would I go about instantiating from a string like this?
p.S>.there IS a prefab with that name ready to be instanced.
Answer by poncho · Dec 31, 2010 at 03:31 PM
if that object exist on your scene, you can try
Instantiate(Gameobject.Find(gameplaySceneManagerScript.clueNodeArray[x].model.name));
hope it works good luck
Answer by Jessy · Dec 31, 2010 at 03:41 PM
No. Instantiate is not for instantiating strings. It is for putting objects into the Hierarchy. You can't see a string in the Hierarchy. A string is not a Game Object. Instantiate the prefab itself, which is a Unity Object.
If you need to load things based on name, then use the Resources folder and corresponding methods.