Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sharmajeenit2000 · Feb 22, 2019 at 12:55 PM · unity 5destroydestroy objectdestroy-clonesdestroyimmediate

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Feb 22, 2019 at 04:47 PM 0
Share

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.

avatar image sharmajeenit2000 Bonfire-Boy · Feb 28, 2019 at 04:18 AM 0
Share

Thanks, I changed the code and it works nicely.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sharmajeenit2000 · Feb 28, 2019 at 04:19 AM 0
Share

@Ymrasu thank you for the answer, now its all clear!

avatar image
1

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sharmajeenit2000 · Mar 01, 2019 at 03:33 PM 0
Share

@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?

avatar image Spike_Felion sharmajeenit2000 · Mar 01, 2019 at 03:41 PM 0
Share

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

avatar image sharmajeenit2000 Spike_Felion · Mar 02, 2019 at 01:44 AM 0
Share

Thank you for the answer. And @Spike_Felion can you please share how, did you learn unity.

avatar image Bunny83 sharmajeenit2000 · Mar 01, 2019 at 03:48 PM 0
Share

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.

avatar image sharmajeenit2000 Bunny83 · Mar 02, 2019 at 01:47 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

199 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges