- Home /
Do I need to destroy objects I'm not using?
I am making a 2d birds eye view exploration game. (i am new to unity btw) For now my world is a fixed size, and i am using tiles to build the land. the size is 2000x2000 tiles, a big square. each tile is an object and the camera can only see about 25 tiles at a time. so to my question. should I, do I need to destroy the tiles that are far away from my camera and recreate them when the camera is close enough? I ask because I am not sure what Unity will do for me in this case. all tips, help, suggestions, and council is welcome thanks in advance.
Answer by scipiothegreat · Feb 04, 2014 at 01:25 AM
Have you considered object pooling? Instead of completely destroying a tile and making a whole new one, you could take an old, and change it to whatever new tile you need. This means you have to make your tile objects changable at runtime, but will keep you from instantiating so many tiles. Just make a class that holds references to tiles that are no longer needed and gives them out when requested.
Answer by Klarax · Feb 03, 2014 at 11:13 AM
I think this varies. If its the same model/sprite or whatever, the item should batch so its not a big deal.
I would leave it as it is now, and keep an eye on stats when game is running, if fps drops, then maybe look into it
I have 6 different tiles that the world can be made up from, plus having objects like trees and animals in the world. it would not be a problem for me as far as being capable to create the objects needed as i get close to them each time, but i was not sure as far as the toll it might be on the game or if creating and destroying is not intense. ill looking to some tests as you suggest. Im still curious on weather or not creating and destroying is expensive. thanks ill update and I find more out
Answer by Fabkins · Feb 03, 2014 at 10:19 PM
I think you need to consider a different approach to the overall problem.
Sound like you need to use Terrains.
http://docs.unity3d.com/Documentation/Components/script-Terrain.html
This is interesting. $$anonymous$$y initial plan was to make this all 2d with sprites, but I can see how this would help build a world in 3d. ill look at this more and see if it might be worth switching to 3d. is there something similar to this for 2d worlds?
Well another approach is to create a mesh by hand. This actually what I've done on one project. Problem is 2000x2000 tile map is going to many vertices. The limit is 65k and that size of map would require 4 million of them (if you not re-using vertices). (just tried it and it broke :) )
ps , when I originally made my map I used object. $$anonymous$$y map was only 100x100 objects and it took a while to initialise. You are going to try to create x400 times that many objects, I think Unity will grind to a halt , its going to work at all.
Yep, it broke big style. It swallowed 4GB of memory and crashed Unity.
yeah i made a for loop to create that many and didnt want to finish waiting for it to load. so I am thinking that I will have about 1000 of them created and create more as I get closer to the edges and destroy the ones out side a given radius. right now I am keeping the definition of a tile in an int and I am using byte masks to read the bits (as far as what type of land, if it can be walked through, or any other items on the tile) so the bottleneck is rendering the tiles and not having that large of a world. - do you think that creating 50 tiles every 2 seconds (2 seconds for walking speed across tiles) will be taxing if I'm also destroying that many each time?
Answer by ProGrahamCracker · Feb 04, 2014 at 09:21 PM
OK! after running some tests and doing performance and system load testing, object pooling is the way to go. create and destroy are very expensive when compared to just reusing objects. I was looking for quick and dirty at first but object pooling is easy and sophisticated. Not to mention that my first language was C, so I naturally care about memory and its allocation. Thanks a lot to all of you that helped.
Your answer
Follow this Question
Related Questions
Destroy all objects in the scene? 0 Answers
How to prevent objects instantiating between scenes? 4 Answers
Destroy GameObject behind the Main Camera 2 Answers
C#: Changing current position of an object in y. 1 Answer
Game Object Not Working Properly 0 Answers