does not destroy prefab

Hi,

I am trying to create a dynamic prefab loading:
you can select items from menu, and whenever a new item is selected - the old prefab is destroyed and a new one is instantiated.

I was trying:

  1. “newPrefab = Instantiate(Transform, vector, vector)”
    ERROR: when I try to “Destroy(newPrefab)” I get error message: “Can’t remove component. Can’t remove Transform because MeshFilter, BoxCollider, MeshRenderer depends on it”
    RESULT: the new prefab is instantiated on top of the old one so I get as many prefabs as I select but none is destroyed.
    NOTE: for some reason I get this message only every other try (selection 1 - no error msg, selection 2- error msg, 3-no 4-error …) - no matter which of the items is selected.

  2. “InstantiatePrefab(GameObject)”
    ERROR: “Unknown identifier: ‘InstantiatePrefab’”

BTW - currently the prefabs are all ‘cube 3D text’ attached to it. the difference between prefabs is the text content.

I don’t get how am I supposed to instantiate a Game Object and destroy it?
PLEASE HELP… ANYONE…?

Hi sigal,

judging by the error messages you posted you are trying to destroy the Transform component instead of the GameObject, which earned you the protests of other components that rely on Transform.

Also, InstantiatePrefab is a method of the EditorUtility class, which is only needed when customizing editor behaviour, so Instantiate is in fact the way to go.

Since you didn’t post your code I guess that you instantiate a Transform component instead of a prefab. Here’s how it’s done:

var myPrefab: Transform;
var position: Vector3 = Vector3.zero;
private var instance: Transform;

function Start()
{
	if (myPrefab)
	{
		instance = Instantiate(myPrefab, position, 
				Quaternion.identity);
	}
	else
	{
		Debug.Log("Error: You must assign a prefab to " 
			+ "myPrefab in the inspector!");
	}
}

function OnMouseUp()
{
	if (instance)
	{
		Destroy(instance);
	}
}

(I didn’t test the code, but it should work.)

Want to learn more? Here’s an in-depth article on instantiating prefabs: http://unity3d.com/support/documentation/Manual/Instantiating%20Prefabs.html

Hi newbrand

I found my problem:

same as you do here - I first declared the prefabs as “Transform” instead of “GameObject”.
When I got the error message I changed the declaration on the code, but I did not re-assign the prefabs to the code, so I ended up using Transforms instead of GameObjects.

Thanks anyway…
:slight_smile: