- Home /
Can't Destroy Instantiated Prefab
Hey all,
I'm sure that this has been asked another way but not in any way that I comprehend as being similar, so excuse that.
I'm trying to do a base builder, am relatively new to Unity, and have a problem destroying the 'tent' object that I'm creating in order to replace it with a new, upgraded tent model.
Here is the script that is on the empty container gameobject 'tent':
public class Tent : MonoBehaviour
{
int tier = 0;
public GameObject[] models = new GameObject[3];
public GameObject currentModel;
//This is a proxy solution to having the building appear as the lowest tier. Need a way to save.
void Awake()
{
currentModel = Instantiate(models[0], new Vector3(this.transform.position.x,
this.transform.position.y, this.transform.position.z), Quaternion.identity) as GameObject;
}
public void UpgradeTent()
{
if (tier < 3)
{
tier++;
Destroy(currentModel);
currentModel = Instantiate(models[tier], new Vector3(this.transform.position.x,
this.transform.position.y, this.transform.position.z), Quaternion.identity) as GameObject;
}
}
}
It should instantiate the tent model (another empty game object with loads of models as children) upon awake. Then when the upgrade function is called, ditch the old model and replace it with the swanky new model from the array.
Both the tent object (with the tent script on it) and the tent model are prefabs, which I'm pretty sure is a big part of the issues.
Thanks in advance for any help.
Where are you calling the UpgradeTent function? and does the new tent spawn as it's supposed to? It shouldn't matter that both are prefabs.
It could matter if the UpgradeTent function was being called on a prefab rather than an instance (so your initial question looks like a good one to me).
The other questions I would want to ask are
1) what is actually happening? For example, the question just says that an instantiated prefab (presumably the old current$$anonymous$$odel but it would be better if this was made explicit) isn't being destroyed, but is the new current$$anonymous$$odel being correctly instantiated?
2) are any errors being logged or exceptions being thrown?
In addition to VildNinja's comment I'd suggest checking whether models[...] has GameObjects assigned in the inspector, I know I've missed that a couple of times by accident.
Answer by StephanT · Jan 28, 2016 at 09:45 AM
Just a note to your instantiation code in lines 10 and 20:
currentModel = Instantiate(models[0], new Vector3(this.transform.position.x,
this.transform.position.y, this.transform.position.z), Quaternion.identity) as GameObject;
You're creating a new Vector3 with the properties of transform.position. Well, you can just use that. And the this is redundant, too.
So, to simplify these two lines I suggest using:
currentModel = Instantiate(models[0], transform.position, Quaternion.identity) as GameObject;
Answer by FortisVenaliter · Jan 27, 2016 at 06:39 PM
The code looks good to me.... The only recommendation I can give is that since you're using the same reference variable, it might be a good idea to use DestroyImmediate() rather than Destroy().
The Destroy() function won't actually delete the object until the end of the frame.
Other than that, I would agree with @Bonfire-Boy that you should check to make sure the function is being called, ideally by throwing a Debug.Log in there and ensuring you see it at the right time in the editor log.
Your answer
![](https://koobas.hobune.stream/wayback/20220612060928im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate Pre-fab clone altered on Instantiate 2 Answers
How to destroy an instantiated prefab in C#? 4 Answers
Only 1 of 3 conditions being executed in IF statement?(Solved) 1 Answer
Can't seem to destroy instantiated objects. 1 Answer
How to Instantiate a GameObject from a ScriptableObject piece of script? 0 Answers