• 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
Question by dgnkrnz · Jan 31, 2019 at 08:43 PM · collisionunity 2dcollider2doncollisionenteroncollisionexit

How can i store all colliders of colliders which collide with a prefab?

alt text i have circle prefabs which have circle colliders 2d and rigidbody2d. i want this prefabs to store all colliders of colliders which collide with them. how can i do this? in the referance image, A can only get {B,C,D}. but i want to store all {A,B,C,D,E,F,G} objects in each object. I use collision enter and exit to do this. for example A; i can store the information of {B,C,D} in a list in A object. and i can get the {A,D,E} which collide with B and C. but no more i can access other objects. is there a way of it?

schema.png (34.1 kB)
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by xxmariofer · Jan 31, 2019 at 11:42 PM

hello you can do some recursive function, first create a collision manager that will store all the colliders that are colliding. now you can start the recursive method, the method needs to pass one collider as argument so for example you start calling yourRecursiveMethod(a);

you create a foor loop that iterates over all the collider you have stored in the list, for each collider you compare if that collider has already been added to the list of all existing colliders inside the collision manager, and if its not you will call your same method from there but passing the collider you were comparing as an argument, that collider will do the same over and over with all the colliders untill you find all. remember you must compare if the collider was aded to the list and if it was just skip it, it might sound a bit complex but is no more than 5 or 6 lines of code.

Comment

People who like this

0 Show 8 · 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 dgnkrnz · Feb 01, 2019 at 12:08 AM 0
Share

hi, thanks for your concern. can you give me a pscode code of it? or you just can update this code.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MyScriptOfCollisions: MonoBehaviour {
 
     public List<GameObject> allCollisions = new List<GameObject>();
 
     private void OnTriggerEnter2D(Collider2D collision) {
         if (collision.tag != this.tag) return;
         if (allCollisions.Contains(collision.gameObject)) return;
         allCollisions.Add(collision.gameObject);
     }
 
     private void OnTriggerExit2D(Collider2D collision)  {
         if (collision.tag != this.tag) return;
         if (!allCollisions.Contains(collision.gameObject)) return;
         allCollisions.Remove(collision.gameObject);
     }
 }
 
avatar image xxmariofer dgnkrnz · Feb 01, 2019 at 08:23 AM 0
Share

here is some pseudococde. CollisionManager is an static class with a list of all the colliders. this will add to the Colliders list all the colliders.

 void IteratorMethod(Collider collider)
 {
     CollisionManager.Colliders.Add(collider.gameObject);
     MyScriptOfCollisions collisions = collider.GetComponent<MyScriptOfCollisions>();

     foreach(GameObject nextObject in collisions.allCollisions)
     {
         if(!CollisionsManager.Colliders.Contains(nextObject))
         {
             IteratorMethod(nextObject.GetComponent<Collider>());
         }
     }
 }
avatar image dgnkrnz xxmariofer · Feb 01, 2019 at 08:40 AM 0
Share

now i understand what exactly you mean. but i stuck in the CollisionManager class. why you created another scirpt and stored gameobjects in the list of it? if we use the "allCollisions.Add(collider.gameObject) rather than "CollisionManager.Colliders.Add(collider.gameObject)" will there be problems? sorry, i just want to catch the fish. :)

Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

221 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 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

2DCollider Issues 1 Answer

2D Platformer: Failure to jump through platform 0 Answers

Enemies spawning on top of each other,Enemies spawn on top of each other 0 Answers

Collision 2D Not Working,Colliders Don't Work 0 Answers

Unity Collider2D is causing the game object to disappear upon 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