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 /
  • Help Room /
avatar image
0
Question by BigGiantHead · Jun 10, 2016 at 07:52 PM · instantiatedestroygameobjects

Problem with Destroy function with instantiated Prefabs

Hi!

I have very peculiar problem. In my game, I instantiate a lot of 4 different kinds of objects (from prefabs). The objects have Kinematic Rigidbody Trigger Colliders attached to them.

I use OnTriggerEnter2D() to handle the logic when two of these colliders collide with each other. In the function I just check that if the other collider is with marked with tag "Moving", and if it is, I Destroy it.

 void OnTriggerEnter2D(Collider2D other) {
            
         if (other.gameObject.tag == "Moving")
         {
             Destroy(other.gameObject);
 


Then, I check if the other Gameobject get actually destroyed;

             if(other.gameObject == null)
             {
                 Debug.Log("Object " + other.gameObject.name + " got destroyed");
             }

Then I call the moveThisBall which handles the moving of the ball which the destroyed object touched.

 moveThisBall();
         }

The problem is that everything seems to work fine EXCEPT with one problematic type of object which was instantiated. All other objects work fine. I have checked, double-checked and triple-checked that everything in the original Prefab is just the same way than in other Prefabs that gets instantiated and they are exactly the same (except graphical presentation).

The source of the problem is that when this problematic object enters to the trigger collider of other object, for some reason it doesn't Destroy the problematic gameobject soon enough. Instead, the code gets so far up in the process that this other gameobject gets tagged with "Moving" tag as well and when the program finally starts to Destroy the gameobject, it destroys both objects. Which is not something that I want.

So my problem is two-fold; 1) I don't understand why with this problematic object, the code gets fired in the other object so long that the other gets tagged with "moving". This doesn't happen with other gameobjects. 2) I don't know how to circumvent this problem, i.e. how to make the program Destroy the problematic gameobject before the other gameobject gets tagged with "Moving" tag.

Any help would be highly appreciated!

  • Jukka

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
0
Best Answer

Answer by NoseKills · Jun 11, 2016 at 09:48 AM

In general you should design your code based on the fact that Destroy() doesn't happen immediately. The documentation of Destroy() says the actual destruction is guaranteed to happen before the next Update cycle, nothing else. Checking 'object == null' right after the Destroy() call can definitely give you misleading information.

I guess the difference between objects could be explained by script execution order, but as said, youd be better off flagging your destroyed objects in some way that prevents them from doing anything you don't want them to do.

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

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

68 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

Related Questions

Issue iterating through gameObjects deleting and addding some 0 Answers

destroying instantiated groups of objects 1 Answer

Add Clones of GameObject to List(array) 0 Answers

Destroy attached shield to player (C#) 2 Answers

Instantiate() and Destroy() vs setActive() 1 Answer


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