How can i store all colliders of colliders which collide with a prefab?
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?
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.
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 $$anonymous$$yScriptOfCollisions: $$anonymous$$onoBehaviour {
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);
}
}
here is some pseudococde. Collision$$anonymous$$anager is an static class with a list of all the colliders. this will add to the Colliders list all the colliders.
void Iterator$$anonymous$$ethod(Collider collider)
{
Collision$$anonymous$$anager.Colliders.Add(collider.gameObject);
$$anonymous$$yScriptOfCollisions collisions = collider.GetComponent<$$anonymous$$yScriptOfCollisions>();
foreach(GameObject nextObject in collisions.allCollisions)
{
if(!Collisions$$anonymous$$anager.Colliders.Contains(nextObject))
{
Iterator$$anonymous$$ethod(nextObject.GetComponent<Collider>());
}
}
}
now i understand what exactly you mean. but i stuck in the Collision$$anonymous$$anager class. why you created another scirpt and stored gameobjects in the list of it? if we use the "allCollisions.Add(collider.gameObject) rather than "Collision$$anonymous$$anager.Colliders.Add(collider.gameObject)" will there be problems? sorry, i just want to catch the fish. :)
Your answer
Follow this Question
Related Questions
2DCollider Issues 1 Answer
2D Platformer: Failure to jump through platform 0 Answers
2D Collision Best Practice 0 Answers
CS1525 Unexpected symbol '=', HELP!!! 2 Answers
OnCollisionEnter Problem 1 Answer