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 Kortuga · May 28, 2016 at 05:08 AM · gameobjectdestroy

Destroyed gameobject found with FindObjectsOfType<>?

I'm making a simple level editor for a 2D platformer. When I delete (Destroy()) a tile in the level editor, then try to recalculate the textures for my tile stitching, the remaining tiles behave as if the destroyed tile were still in the scene.

 //Delete the (topmost) object at the mouse position
     void DeleteObject() {
         Collider2D hit = Physics2D.OverlapCircle(Camera.main.ScreenToWorldPoint(Input.mousePosition), 0.0001f, menuMask);
 
         if (hit != null) {
             if (hit.gameObject.GetComponent<Tile>() != null) {
                 Destroy(hit.gameObject);
                 Recalculate();
             }
         }
     }
 
     void Recalculate() {
         Tile[] tiles = FindObjectsOfType<Tile>();
         foreach (Tile t in tiles) {
             t.RecalculateTexture();
         }
     }

In the above code, "hit.gameObject" is included in the array "tiles" after being destroyed, and the other tiles in "tiles" don't re-stitch themselves properly as a result. What do I need to do differently to destroy the hit tile completely before Recalculate() is called?

Comment
Add comment
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

1 Reply

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

Answer by ninja_gear · May 28, 2016 at 05:23 AM

When you call Destroy a GameObject, Unity will wait until the end of the frame (or cycle I can't remember which but lets say frame to be safe) before it removes that object from the scene and releases its memory. If you ask Unity for a component from that GameObject before the end of the frame that you called Destroy on it, Unity will save that component and not release its memory while at the same time destroying all other parts of said GameObject. If you want to Recalculate() right after the destroy use a Coroutine to break the two commands apart by a frame:

     //Delete the (topmost) object at the mouse position
     void DeleteObject()
     {
         Collider2D hit = Physics2D.OverlapCircle(Camera.main.ScreenToWorldPoint(Input.mousePosition), 0.0001f, menuMask);
 
         if (hit != null && hit.gameObject.GetComponent<Tile>() != null)
         {
             StartCoroutine(destroyThenRecalculate(hit.gameObject));
         }
     }
 
     void Recalculate()
     {
         Tile[] tiles = FindObjectsOfType<Tile>();
         foreach (Tile t in tiles)
         {
             t.RecalculateTexture();
         }
     }
 
     IEnumerator destroyThenRecalculate(GameObject hitObj)
     {
         Destroy(hitObj);
         yield return null;
         Recalculate();
     }
Comment
Add comment · Show 4 · 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 Kortuga · May 28, 2016 at 01:14 PM 0
Share

This actually doesn't work for me, I have no idea why not though. I do know for a fact that my "RecalculateTexture" method works perfectly because the tiles stitch properly when instantiating, just not when destroying.

The pictures below illustrate the problem: as I draw the tiles onto the grid, they stitch together dynamically. When I delete the rightmost one (second picture), the second-to-rightmost tile still thinks that the deleted tile is there. This is with your code enabled.

http://i.imgur.com/e26WAXm.png http://i.imgur.com/AU$$anonymous$$Y1eY.png

I've also tried to just enabled=false the box collider on deleted tiles (not destroying them) to see whether the other tiles still calculate their neighbors the same way, and they do (still no proper stitching), even though the collider is successfully disabled from what I can tell.

avatar image ninja_gear Kortuga · May 28, 2016 at 04:46 PM 0
Share

Well, to me it sounds like Tile.RecalculateTexture() might be the issue but we can zero in harder on the problem. What does this Debug.Log tell you:

      void Recalculate()
      {
          Tile[] tiles = FindObjectsOfType<Tile>();
 
          Debug.Log(tiles.Length);
 
          foreach (Tile t in tiles)
          {
              t.RecalculateTexture();
          }
      }
avatar image Kortuga ninja_gear · May 28, 2016 at 08:34 PM 1
Share

That logs the correct amount of tiles. Which makes me think it's probably Tile.RecalculateTexture() that's acting up after all. But that's strange in itself, because it stitches them dynamically when the user paints the tiles onto the grid, just not when they erase them. I assumed the logic for recalculation would be the same, but I suppose I'll take another look (thanks for the help, by the way :D).

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

GameObject.FindGameObjectsWithTag still finding destroyed object (C#) 1 Answer

Help, Missing Object 1 Answer

Destroy GameObject won't destroy 1 Answer

destroy gameobject when collider is fast enough? 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