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 Ochreous · Aug 08, 2016 at 09:08 PM · c#gameobjectinstantiaterandom

C# GameObjects Instantiate Into Each Other Issue

I have this script that randomly generates prefabs within a 2D box collider. However I've noticed that sometimes the prefabs spawn into each other. Is there anyway I could prevent this from occurring? I'm not really sure where to start since all of the gameobjects spawn at the same time.

 using UnityEngine;
 using System.Collections;
 
 public class GeneratePrefabsWithinCollider : MonoBehaviour {
     public BoxCollider2D playerCollider;
     public GameObject[] placeableObjects;
     public GameObject currentObject;
     public int numberOfObjects;
 
     public void Start () {
         for(int i = 0; i < numberOfObjects;i++)
         {
             for(int j = 0; j < placeableObjects.Length;j++)
             {
             currentObject = Instantiate(placeableObjects[j],GeneratedPosition(),Quaternion.Euler(0, 0, Random.Range(0f, 360f))) as GameObject;
             currentObject.transform.parent = transform; 
             }
         }
     }
     
     Vector3 GeneratedPosition()
     {
         float x,y;
         x = UnityEngine.Random.Range(transform.position.x, transform.position.x + GetComponent<Collider2D>().bounds.size.x) - GetComponent<Collider2D>().bounds.extents.x;
         y = UnityEngine.Random.Range(transform.position.y, transform.position.y + GetComponent<Collider2D>().bounds.size.y) - GetComponent<Collider2D>().bounds.extents.y;
         return new Vector3(x,y,1);
     }
     
     void OnTriggerEnter2D(Collider2D other) {
         if(other == playerCollider){
             for(int i = 0; i < transform.childCount; i++){
                 if(transform.GetChild(i).gameObject.activeSelf == false){
                     transform.GetChild(i).gameObject.SetActive(true);
                 }
             }
         }
     }
     void OnTriggerExit2D(Collider2D other) {
         if(other == playerCollider){
             for(int i = 0; i < transform.childCount; i++){
                 if(transform.GetChild(i).gameObject.activeSelf == true){
                     transform.GetChild(i).gameObject.SetActive(false);
                 }
             }
         }
     }
 }



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 DiegoSLTS · Aug 08, 2016 at 09:34 PM

They're not instantiated at the same time, they're instantiated one after another. You can check if an object overlaps any of the created objects, move it to another random position and check again, and repeat until it's in a good position. If they have colliders you can use physic to check, or you can make a fast check for distance if the objects are similar. This has a problem, if you instantiate too many objects you might get stuck if there's no valid position, and it's hard to know when that actually happens.

Comment
Add comment · Show 3 · 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 Ochreous · Aug 10, 2016 at 04:57 AM 0
Share

Okay so I've added a script to the instantiated prefabs which checks to see if another gameobject has intersected them and to recalculate their position if so. However the gameobjects are still intersecting each other and some of them are spawning outside of their parent's collider. What am I doing wrong here?

 using UnityEngine;
 using System.Collections;
 
 public class IntersectCheck : $$anonymous$$onoBehaviour {
 public BoxCollider2D parentCollider;
     // Use this for initialization
     void Start () {
     parentCollider = transform.parent.GetComponent<BoxCollider2D>();
     }
     
     // Update is called once per frame
     void OnTriggerEnter2D(Collider2D other)
     {
         if(GetComponent<BoxCollider2D>().bounds.Intersects(other.bounds)){
         transform.position = RecalculatedPosition();
         }
     }
     Vector3 RecalculatedPosition()
     {
         float x,y;
         x = UnityEngine.Random.Range(transform.position.x, transform.position.x + parentCollider.bounds.size.x) - parentCollider.bounds.extents.x;
         y = UnityEngine.Random.Range(transform.position.y, transform.position.y + parentCollider.bounds.size.y) - parentCollider.bounds.extents.y;
         return new Vector3(x,y,1);
     }
 }
 
avatar image DiegoSLTS Ochreous · Aug 11, 2016 at 06:15 PM 0
Share

I doubt "OnTriggerEnter2D" is being called at all, have you debugged that? If you add a Debug.Log line there is anything printed in the console?

It looks weird, you have an OnTriggerEnter3D method but it looks like the object with that script doesn't have a collider. Also, those methods are only called if at least one of the objects that collided has a rigidbody.

Anyway, you don't need that, you can check the intersection right after you create each game object. I'll mix some of your code with pseudocode:

 public void Start () {
      for(int i = 0; i < numberOfObjects;i++)
      {
          for(int j = 0; j < placeableObjects.Length;j++)
          {
              currentObject = Instantiate(placeableObjects[j],Vector3.zero,Quaternion.Euler(0, 0, Random.Range(0f, 360f))) as GameObject;
              //keep moving the object until it's in a valid position
              do {
                  move currentObject to a random position;
              } while ( !validPosition(currentObject) );
              currentObject.transform.parent = transform; 
          }
      }
  }

 bool validPosition(GameObject current) {
     foreach ( GO in already instantiated game object) {
         if ( current != GO and current's collider intersects GO's collider)
             return false;
     }
     return true;
 }

avatar image Ochreous DiegoSLTS · Aug 13, 2016 at 05:50 AM 0
Share

You're right, one of the instantiated gameobjects was missing a rigidbody and that's why it wasn't being returned.

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

209 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to randomly spawn three non repeating gameobjects from an array? 2 Answers

C# 2d Instantiate object randomly around gameobject 1 Answer

What is the best way to instatiate an array of connected GameObjects? 0 Answers

Random instantiate at same frame with each instantiate having unique random direction 1 Answer

instantiate random prefabs based on player camera distance 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