- Home /
Difference between Destroy and Destroy immediate (In this case).
Hey I am trying to make a tic tac toe game in unity, so i am instantiating my X and O's, so when I need to start a new game, I need to clear out all the previously instantiated X and O's. So, I am using the following method to do so :-
public void NewGame()
{
for (int i = 0; i < 9; i++)
{
p1WinCond[i] = 0;
p2WinCond[i] = 0;
pos[i].gameObject.SetActive(true);
}
for (; buttonPresses != 0; buttonPresses--) //to count the no. of X and O's
{
DestroyImmediate(GameObject.Find("O(Clone)"));
DestroyImmediate(GameObject.Find("X(Clone)"));
}
this.transform.GetChild(4).gameObject.SetActive(false);
GameOver = false;
player1Wins.SetActive(true);
player2Wins.SetActive(true);
}
Ignore the rest of the code, so, when I use Destroy func. only the first X and the first O are being destroyed, the rest remain as it is, despite the loop running its full course. But, when I use Destroy immediate it works as desired all of them are deleted.
wanted to know why is it so?
Also, any alternative approach to the problem is appreciated too.Thank You.
I$$anonymous$$HO it's much better to do this kind of thing without using Find at all. When you instantiate your X's and O's, add them to a container of some sort. When you want to destroy them all, just use the the container.
Thanks, I changed the code and it works nicely.
Answer by Ymrasu · Feb 22, 2019 at 01:22 PM
Destroy() just marks an object to be neatly destroyed at the end of the frame. DestroyImmediate as the name suggests, destroys the object right then and there with some overhead.
In your loop when you Find the objects, it only returns the first ones it finds. So when you use Destroy(), you just keep marking the first ones for destroy and never get to the rest. While using DestroyImmediate() it destroys them in that loop, so that in the next loop the Find sees the next objects.
It is better to use Destory() only in your game. If the O or X objects have a script on them you can use FindObjectsOfType to get all of them at once in an array, then you can use Destory() on them. You can also use tags. Put a tag on the prefab, then you can use FindGameObjectsWithTag
@Ymrasu thank you for the answer, now its all clear!
Answer by Lynkfox · Feb 28, 2019 at 05:01 AM
You have your answer already,but as an aside: Generally, if you can you might want to avoid using Destroy () too much. In a small tic tac toe game, k. No biggy.Not a lot of objects. If you start using destroy when every one of thousands of bullets as they fly across your screen, it becomes really bad really fast.
it's better practice to move the object you want to destroy off screen, into a 'pool' and hide it. Then when you need one of them again, you just bring that back instead of instantiating a new one. Again, in a small little game of tic tac toe, no big deal if you do it the destroy way. But as your projects get bigger, this would be a better practice.
@Lynkfox but won't the objects put aside increase the cpu usage, as they would be rendered by unity anyways, despite, not being in the scene?
That is why you set the gameObjects active to false. that way their update doesn't get called and they won't be rendered.
The reason you pool objects when there will be a lot is due to the amount of garbage that accumulates constantly instantiating and destroying.
The processing of the garbage collector and of having to instantiate the objects makes pools more efficient, but ya for a tic tac game ur fine :P
Thank you for the answer. And @Spike_Felion can you please share how, did you learn unity.
Yes and no. If you don't deactivate the gameobject, yes it will still be processed like any other object in the scene. However since it's off screen it will already be frustum culled and not rendered at all.
Destroying and instantiating objects is one of the most expensive operations. They also need to allocate new memory or leave garbage behind that need to be cleaned up be the garbage collector. On some platforms (like mobile) the garbage collector is a huge performance killer. That's why you want to avoid allocating / generating too much garbage.
Even activating / deactivating gameobjects is not free. This highly depends on the complexity of the object and what components it uses. Any physics based object need to be registrated / unregistrated by the physics engine when activated / deactivated. If you have a huge number of objects (around 1000 per second for example) which you're going to activate / deactivate it can have quite a bit of a performance hit. In such cases it's often cheaper to just move them out of the view frustum. The overall performance will stay constant and that's the actual important thing. If the game only runs at 30fps that's not a real problem as long that's a s$$anonymous$$dy 30fps. 60fps with GC spikes feels much worse than s$$anonymous$$dy 30fps.
Thank you for clarifying, shockingly this was all new to me! @bunny83 please can you share how did you learn unity, did you go to some sort of ga$$anonymous$$g school or self learning?
Your answer
Follow this Question
Related Questions
Cannot remove sprite using destroy 1 Answer
Destroy Objects after a set time 2 Answers
Destroy Clone when colliding with 2d box collider 0 Answers
Destroy(this.gameObject) vs Destroy(gameObject) 1 Answer
Destroy function not documented? 2 Answers