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 Sithell · Jul 06, 2017 at 07:18 PM · unity 5coroutinecollider2d

Protection against intersection of generated objects.

On the scene during the game, game objects are generating randomly.
We need to make sure that the created objects do not intersect (that is, do not overlap each other).
At the moment I'm using this:

 // Repeat until there is a free space or until the number of attempts is more than 10
     for (int attempt = 0; attempt <10; attempt ++)
     {
         objects [i] = Instantiate (objectToGenerate, position, Quaternion.identity, parent);
     
         if (objects [i] .GetComponent <CollideManager> (). IsTriggered)
         {
             Destroy (objects [i]);
         }
         else break;
     }

The CollideManager component is attached to objectToGenerate. The isTriggered variable is true if the object intersects with another object. Here is the component code:

     public class CollideManager: MonoBehaviour {
     
         public bool isTriggered = false;
     
         private void OnTriggerEnter2D (Collider2D other)
         {
 
             if (other.gameObject.tag == "Static")
             {
                 IsTriggered = true;
             }
         }
     }

But all this code does not work.
I guess that it's because unity does not have time to process a collision of a new object with the old ones in a zero period of time between this line:

 objects [i] = Instantiate (objectToGenerate, position, Quaternion.identity, parent);

And this:   

  if (objects [i] .GetComponent <CollideManager> (). IsTriggered)

So I decided to change it all into a coroutine and add a delay:     

 objects [i] = Instantiate (objectToGenerate, position, Quaternion.identity, parent);
 
     yield return new WaitForSeconds (0.5f);
 
     if (objects [i] .GetComponent <CollideManager> (). IsTriggered)
     {
         Destroy (objects [i]);
     }
     else break;

But now

  1. The game began to hang.

  2. Objects appear right in front of the user, and in the most unexpected and inconvenient places.


I also tried to use WaitForFixedUpdate instead of WaitForSeconds, but it didn't work: objects were intersected with each other.
How to make it work?

P.S. Sorry for my English :)

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 RobAnthem · Jul 06, 2017 at 07:34 PM 0
Share

Have you considered using the overlapping sphere function to check based on the bounds of the object whether or not something else exists in its place, aside from terrain of course. Then you can avoid redundant instantiations and don't need to waste memory resources by destroying them.

avatar image Sithell RobAnthem · Jul 06, 2017 at 07:38 PM 0
Share

Do you mean this? https://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html
No, I haven't, but I'll try. thanks)

0 Replies

· Add your reply
  • Sort: 

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

133 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

Related Questions

Skip function 1 Answer

I'm having trouble with a coroutine delay 2 Answers

Platform Collider Not Working When Enemy Collides With It 1 Answer

Coroutine called in different instances 2 Answers

How do i stop a co-routine that is running from a different object but with the same script? 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