- Home /
How to Destroy Terrains (destroy(this.gameobject) not working)
Hi guys!
So I currently have a game that's set up to randomly instantiate game objects from an array for a endless runner. I've recently tried to make it so that terrains can be instantiated as I'm not the best 3D modeller in the world.
I've made each terrain a prefab, which is great and allows for the terrains to be generated indefinitely, which is fantastic! But as you pass the terrain and it goes out of the camera, they aren't being destroyed. I usually use OnBecameInvisible to destroy a object, but this really isn't working for the terrains, and I certainly don't want terrains continuing to exist past the camera and hogging all the memory.
Any advice would be greatly appreciated!
Answer by RalphTrickey · Jul 29, 2013 at 10:21 PM
The simplest way would be use a queue. That is, keep a queue of objects and each time you push one on, if there are more than X components, pop one off the end and destroy it. That limits memory and ensures that you can have no more than X components.
Your answer
Follow this Question
Related Questions
How do I Destroy a Child after Instantiating it? 1 Answer
Should I destroy and create objects from prefabs to create a "copying" gameplay mechanic? 0 Answers
The object of type 'Transform' has been destroyed but you are still trying to access it. 1 Answer
Destroy Without A Collision Or Trigger 2 Answers
How can I debug gameObject destruction? 2 Answers