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 G3R1 · Jun 02, 2017 at 05:17 PM · coroutineoncollisionentersetactive

[SOLVED]Coroutine method called in OnCollisionEnter() not being called for each collision on chronological order ?!

Hi,

I am using an object pool to avoid intantiate and destroy being called hundred of times during game play. Everything work's fine using the object pool to activate and deactivate object pool objects. The problem is when I use a coroutine in order to delay the deactivating of the objects at least 1 second. This coroutine is called inside OnCollisionEnter() method and if I don't call this coroutine the objects deactivate immediately when the collisions happens. The collision happens between the object pool objects and the ground and if an object falling from sky touches the ground the coroutine starts yielding for one second and gameobject.setactive(false) is exectued. Now, if there is only one object activated from object pool everything works fine and this object is deactivated after 1 second. But if there are two objects activated one after another in different times and one touches the ground suppose 0.5 seconds before the second object, the coroutine starts yielding for 1 second and after the coroutine finishes the second object gets deactivated and the first objects stays on the ground withouth being deactivated. I want the object to be destroyed on a chronological order after they touched the ground, the first after 1 second, the second after 1 second and so on.

Here you have the script I am using on the ground when collision happens: public class DeactivateObjects : MonoBehaviour {

     Collision col;
 
     void OnCollisionEnter(Collision coll)
     {
         if (coll.gameObject.tag == "stone")
         {
             col = coll;
             StartCoroutine ("WaitOneSecToDeactivateTrash");
         }
     }
 
     IEnumerator WaitOneSecToDeactivateTrash()
     {
         yield return new WaitForSeconds (1f);
 
         col.gameObject.SetActive (false);
     }
 
 }

If anyone has encountered the same problem or knows a solution for this...tell me. :) Thank you everyone, Gerald.

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

Answer by ShadyProductions · Jun 02, 2017 at 05:43 PM

That's because you set your coll to a col variable u store somewhere, and then call that in your IEnumerator. which becomes the 2nd coll when the other object hits so it removes the 2nd and the first one is lost. You can fix this by passing the collider object to the IEnumerator like so:

      void OnCollisionEnter(Collision coll)
      {
          if (coll.gameObject.tag == "stone")
          {
              StartCoroutine (WaitOneSecToDeactivateTrash(coll));
          }
      }
  
      IEnumerator WaitOneSecToDeactivateTrash(Collision coll)
      {
          yield return new WaitForSeconds (1f);
  
          coll.gameObject.SetActive (false);
      }
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 G3R1 · Jun 02, 2017 at 08:53 PM 1
Share

Thank you very much for your help ShadyProductions. I am really bad at program$$anonymous$$g and you really helped me with that. ;)

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

Load Children One at a Time 1 Answer

Problem with Object Pooling and Deactivating Objects.!! 1 Answer

Powerup issue - Speed 1 Answer

Changing transform.localScale but OnCollisionEnter isn't executed 1 Answer

Invincible after collision 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