Delete gameobject clone after a new one spawnes.
I'm trying to instantiate gameobjects on UI menu panel, it works well but whenever i refresh the menu it keeps spawning new clones. I want to delete clone object if a new one spawns, so i can keep only one at once.
Answer by wes4ray · May 09 at 09:18 PM
Check out procedural generating objects videos all over youtube but the key to get it to work for me is to have a saved bool that is true if the object is there & false if its not. Then reference that saved bool so your code knows if it needs to spawn or delete the object. It should trigger the bool to false when deleted and setting the bool to true when spawned. Try using Visual scripting or Bolt if you are new to coding
Answer by mnrspec · May 11 at 04:07 PM
Thanks mate, i could fix the problem just adding an if statement.
if (clone== false) { clone = Instantiate(goPrefab), gameObject.transform).GetComponent(); } something like this. Tried to change variable names to be clear. Sharing in case of somebody stucks with the same issue.
Your answer
![](https://koobas.hobune.stream/wayback/20220613060336im_/https://answers.unity.com/themes/thub/images/avi.jpg)