- Home /
How to destroy intiated gameobject prefab from controller script
Hi,
I have empty game object with a prefab controller script attached. The script is used to instantiate a prefab in the scene in random positions after every few seconds. I want to destroy that prefab if no interaction is made to it by the player or the enemy. But when I try to destroy the gameobject it shows error "Destroying assets is not permitted to avoid data loss." I can solve this by adding a script directly to the prefab but I want to destroy it from the contorller script.
public class HerbController : MonoBehaviour {
public GameObject Herbs;
public Vector3 HerbsPosVals;
// Use this for initialization
void Start () {
InvokeRepeating ("SpawnHerbs", 1, 5);
Destroy(Herbs, 5); /// the code here is throwing error
}
void SpawnHerbs(){
Vector3 HerbsPos = new Vector3 (Random.Range(-HerbsPosVals.x, HerbsPosVals.x), HerbsPosVals.y, Random.Range(-HerbsPosVals.z, HerbsPosVals.z));
Instantiate (Herbs, HerbsPos, Quaternion.identity);
}
}
Please anyone with the C# solution or I am doing it wrong.
Answer by Landern · Dec 26, 2014 at 05:46 PM
Don't destroy in Start.
Get a reference to the instantiated object and use that reference to destroy it. Instantiate does return an Object type, i'm casting it to ensure i don't have to worry about answering questions about the difference between a Unity Object type and .net's Object type.
public class HerbController : MonoBehaviour {
public GameObject Herbs;
public Vector3 HerbsPosVals;
// Use this for initialization
void Start () {
InvokeRepeating ("SpawnHerbs", 1, 5);
}
void SpawnHerbs(){
Vector3 HerbsPos = new Vector3 (Random.Range(-HerbsPosVals.x, HerbsPosVals.x), HerbsPosVals.y, Random.Range(-HerbsPosVals.z, HerbsPosVals.z));
GameObject go = (GameObject)Instantiate (Herbs, HerbsPos, Quaternion.identity);
Destroy(go, 5); // destroy after 5 seconds
}
}
Answer by hav_ngs_ru · Dec 26, 2014 at 03:05 PM
Its all messed up in your script :) What do you want to destroy a prefab for?? Why do you do it in Start?
And I guess you confuse what is Prefab and what is Instantiated object.
Prefab - is an Idea of object, not the object. It does not exists in scene and cant be destroyed (I'm a bit simplifies the situation, but it should be more clear). And when you instantiate an object (or objects) from prefab - you can move this object, you can interact with it, and you can destroy object.
So, you should destroy objects, not prefab. You could store a link to each instantiated object (and destroy it from your spawner controller), or you could make a controller for your herbs and each herb will destroy itself when needed).
Ok so I will need to attach script to my herb object and save it as prefab but that is what I dont want to do. Is there any way I can do it from the Herb controller script above. and can you please throw a light what is wrong with the script beside the destroy herb line, there are only two methods the one is "start" and the other is for spawning the herbs.
By destroying I mean the spawned object not the prefab. How can I destroy it after every few seconds when it is spawned.
Your answer
Follow this Question
Related Questions
Destroying objects with the same name that are touching eachother. 1 Answer
Destroy gameobject component that inherits from a certain class 3 Answers
Pooling everything...or not? 1 Answer
Is it possible to destroy a gameobject from a child gameobject's script? 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers