- Home /
Problem instantiating a prefab with static typing
Since I'm coding for iOS, I gather I need to use static typing in my scripts. I have a function in one script which instantiates a prefab (a dust cloud). I want to retain the instantiated GameObject as a variable in the main script because I want THAT script to decide when to destroy the dust cloud. Here's a simplified version:
var dust : GameObject; var dustCloud : GameObject; // this is the prefab
function MakeDust() { dust = Instantiate(dustCloud, transform.position, Quaternion.identity); }
function KillDust() { Destroy(dust); }
I get this error:
BCE0022: Cannot convert 'UnityEngine.Object' to 'UnityEngine.GameObject'.
The error goes away if I change the first line to:
var dust : Object;
But then, I get this error:
BCE0023: No appropriate version of 'UnityEngine.Object.Destroy' for the argument list '(Object)' was found.
Answer by Tetrad · Feb 23, 2011 at 07:49 PM
Instantiate
returns an Object
. What you need to do is cast it to a GameObject
. I'm not familiar with the JS syntax, but try doing this:
dust = (GameObject)Instantiate(dustCloud, transform.position, Quaternion.identity);
That's C#; JS syntax is "`dust = Instantiate(dustCloud, transform.position, Quaternion.identity) as GameObject;`"
Your answer
Follow this Question
Related Questions
Instantiate problem 2 Answers
Instantiating prefab at child (spawnlocations are arrays) 2 Answers
need help with instantiate'ing a Rigidbody 1 Answer
Checking if object intersects? 1 Answer