- Home /
Check collision from another object's script
Is ther a way to know what another object is colliding with? I have a bunch of child objects, each with a trigger collider. I want the parent to be able to know which children are colliding with an object. Here is my parent's script:
foreach (GameObject child in transform) {
if (child. /* on trigger stay includes an object with tag "taken", */){
Debug.Log (child.transform.name + " is colliding with taken");
}
}
Is there a way to get a list of another object's collisions from script?
Answer by ethestel · Apr 06, 2015 at 03:48 PM
Hello, iterating through all childs on each physics update is not quite optimized. Try notifying parent only when a collision occurs. Try attaching this script to your parent object:
using UnityEngine;
public class ParentScript : MonoBehaviour {
public void CollisionDetected(ChildScript childScript)
{
Debug.Log("child collided");
}
}
and this one to every child which are colliding
using UnityEngine;
public class ChildScript : MonoBehaviour {
void OnCollisionEnter(Collision collision)
{
transform.parent.GetComponent<ParentScript>().CollisionDetected(this);
}
}
Please be aware that childs must be nested inside parents transform.
Answer by 237641 · Apr 06, 2015 at 03:26 PM
//on child script
void OnTriggerEnter()
{
collided = true;
}
//on parent script
foreach(Gameobject child in transform)
{
if (child.collided)
Debug.Log(child.transform.name + " collided with taken");
}
Answer by meat5000 · May 09, 2016 at 01:35 PM
You can use ref keyword in C# or use GetComponent to pass the reference to the data.
Answer by RandomGuy403 · Aug 15, 2021 at 11:46 PM
If your child is buried deep within your objects, you can use this instead. As you may have notice, the parent registers their method via delegate to each child, while the child just triggers the said delegate. if you're dealing with multiple colliders, you can also pass multiple children to your parent.
PARENT CLASS
public class CollisionParent : MonoBehaviour
{
[SerializeField]
public List<CollisionChild> colliders;
// Start is called before the first frame update
void Start()
{
foreach(CollisionChild child in colliders)
{
child.collide = onCollide;
}
}
void onCollide(Collider collider)
{
Debug.Log("Collided");
}
}
CHILD CLASS
public class CollisionChild : MonoBehaviour
{
public delegate void Collide(Collider collide);
public Collide collide;
private void OnTriggerEnter(Collider other)
{
collide(other);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613192329im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script sets same value to other script in all objects instead of just one. 1 Answer
Refresh panel with prefab contained value from json that created using array 0 Answers
How can I made a In app Purchase code (google play store) for unity 2019.1.9 ? 0 Answers
where have i went wrong in my script (unity 5) 0 Answers
How To Code Interior/Exterior Cell/Worldspaces Like Fallout/Skyrim 1 Answer