6

I need to instantiate and destroy a prefab on the run. I tried these:

public Transform prefab;     //I attached a prefab in Unity Editor

Object o = Instantiate(prefab);
//using this I cannot get the transform component (I don't know why) so useless

Transform o=(Transform)Instantiate(prefab);
//gives transform and transform component cannot be destroyed

GameObject o=(GameObject)Instantiate(prefab);
//invalid cast

So how to do that?

MarkNinja
  • 31
  • 1
  • 10
Temp Id
  • 387
  • 3
  • 9
  • 15

4 Answers4

3

gives transform and transform component cannot be destroyed

Destroy the GameObject to which the Transform component is attached to:

GameObject.Destroy(o.gameObject);

Instantiate method returns the same type of the object passed as parameter. Since it's a Transform you can't cast it to GameObject. Try this:

GameObject o=((Transform)Instantiate(prefab)).gameObject;
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
3

You don't have to declare your Instance as Object, if you do you get the ancestor object which it has not the transform component.

public GameObject prefab;
GameObject obj = Instantiate(prefab); 

If you want get transform component just type obj.transform.
If you want destroy the object type Destroy(obj);.

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
BilalDja
  • 1,072
  • 1
  • 9
  • 17
  • cannot convert implicitly...so I added (GameObject) there and it ran successfully but I need to check a few more things to verify and then implement it – Temp Id Aug 12 '13 at 13:45
1

Your codes does not make sense..

public Transform prefab;
Object o = Instantiate(prefab);

You are instantiating a Transform? Why dont you try attaching the prefab instead?

You should try:

public GameObject prefab; // attach the prefab in Unity Editor
GameObject obj = Instantiate(prefab);
GameObject.Destroy(obj);
xuanweng
  • 1,939
  • 1
  • 11
  • 10
  • 2
    His code makes perfect sense. The [official documents](http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html) even use the same `public Transform Prefab` notation. – Jerdak Aug 12 '13 at 12:35
  • `public Transform prefab;` is ok, you could declare the variable type as Transform or GameObject, depend on your demand. – Chchwy Aug 13 '13 at 09:47
  • This has bothered me for years, it seems it finally makes sense. "The Parent object will be copied" if a component. Basically if you want to only focus on the transform, copy it. Finally makes sense. I recommend looking up assigning game objects to scripts, unity answers has a nice explanation there on that. – Stephen J Feb 17 '14 at 03:19
1

I noticed the accepted answer is actually wrong.

When using the Instantiate function of the MonoBehaviour class we must specify the type of what we are instantiating. I highly recommend reading Instantiate API reference.


To instantiate a prefab as a GameObject

GameObject g = Instantiate(prefab) as GameObject;

To instantiate a prefab as a Transform and provide a position in 3D space.

Transform t = Instantiate(prefab, new Vector3(1,10,11), new Quaternion(1,10,11,100));

To Destroy a component, which means you can destroy scripts attached to gameObjects as well as rigibodies and other components.

Destroy(g);

or

Destroy(t.gameObject)