- Home /
Question by
paulcmnt · Jun 23, 2012 at 08:34 AM ·
javascriptgameobjectinstantiatecast
How to correctly convert Object to GameObject
In my UnityScript code I need load a prefab. Currently, I'm doing this as
var prefab : GameObject = Resource.Load("MyPrefab")
which gives me a warning about implicit conversions fromObject
toGameObject
. I need it to be aGameObject
because I need to position it based on itsrenderer.bounds.size
. How can I get rid of this warning?When instantiating with
Object.Instantiate
, is it safe for me to convert the returnedObject
toGameObject
? I need to modify some of its attributes when placing it in the world.
Comment
Best Answer
Answer by hijinxbassist · Jun 23, 2012 at 08:41 AM
var prefab : GameObject = Resource.Load("MyPrefab")as GameObject;
That should solve all the problems. You can alter it however you'd like AFTER instantiating it
var myGO=Instantiate(prefab,Vector3.zero,Quaternion.identity);
myGO.GetComponent(SomeScript).someValue=value;
Or you could say
var myGO=Instantiate(Resource.Load("MyPrefab"),Vector3.zero,Quaternion.identity)as GameObject;