Is it possible to check collision from another object?

The question says everything. Can we detect a collision from an object from another object. For example, a collision that happens to a child object of a body that will make the health go down. Thanks for your answers everyone!

Here’s how I solved it. This is the script that is not on the same GameObject as the Collider, but in some parent of it. It will find the first object with a Collider2D and listen to it’s events:

public class ColliderListener : MonoBehaviour
{
	void Awake()
	{
		// Check if Colider is in another GameObject
		Collider2D collider = GetComponentInChildren<Collider2D>();
		if (collider.gameObject != gameObject)
		{
			ColliderBridge cb = collider.gameObject.AddComponent<ColliderBridge>();
			cb.Initialize(this);
		}
	}
	public void OnCollisionEnter2D(Collision2D collision)
	{
		// Do your stuff here
	}
	public void OnTriggerEnter2D(Collider2D other)
	{
		// Do your stuff here
	}
}

And this is ColliderBridge.cs, no need to attach it anywhere:

public class ColliderBridge : MonoBehaviour
{
	ColliderListener _listener;
	public void Initialize(ColliderListener l)
	{
		_listener = l;
	}
	void OnCollisionEnter2D(Collision2D collision)
	{
		_listener.OnCollisionEnter2D(collision);
	}
	void OnTriggerEnter2D(Collider2D other)
	{
		_listener.OnTriggerEnter2D(other);
	}
}

As long as you have a consistent means of checking for it, there shouldn’t be a problem. If it’s a projectile hitting the child object, for instance, the projectile handles the “OnCollisionEnter()” function (or whichever variant) and determines whether the target hit has your script on it. If not, maybe it’s the parent object? If it’s not that either, then it must’ve hit something else.

// C#

void OnCollisionEnter(Collision other)
{
	MyScript otherScript = other.GetComponent<MyScript>();
	if(otherScript == null && other.transform.parent != null)
	{
		otherScript = other.transform.parent.GetComponent<MyScript>();
		if(otherScript == null) // If second test also failed...
		{
			// Probably hit something other than your player
		}
	}
}

Reference for calling parent data: http://docs.unity3d.com/ScriptReference/Transform-parent.html

I did this by creating a public static Boolean variable and set it to true on collision.

other object scripts have access to the Boolean variable.

Yes its indirectly possible but easier in C#

I have a similar problem where I am instantiating a projectile from a prefab. You can attach a collider to the prefab, and add a script like the following:

public class ColliderDetector : MonoBehaviour {

    [HideInInspector]
    public bool collided = false;

    void OnTriggerEnter(Collider other) {
      if (other.gameObject == target) {
        collided = true;
      }
    }

}

Then in your other class, just do a check for it like so

boolean collided  = projectile.GetComponent<ColliderDetector>().collided;

If you need to check at runtime, then an event pub-sub system might be the way to go. There are quite a good number of tutorials online for that.

Just passing through with my solution. add script to any child object u want to pass trigger enter events to parent for. Other events (exits/collisionenter/etc) can be handled in this same class this is just all i typed for an example.
Then just get the child object in the parent, from getcomponentsinchildren or one of the many other ways in unity to get gameObjects/components, and set ur event handler like

colliderpasser.TriggerEnter += MethodToCallOnTriggerEnter;

your method “MethodToCallOnTriggerEnter” will need 2 parameters, GameObject, Collider because this is what the delegate in the script sets it to, this can be any parameters you want really though

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

//attach to any child that you want a parent object to detect collisions for
public class ColliderPasser : MonoBehaviour
{

   //Custom event to send this and the colliding object collider (instead of the normal object, eventargs)
   public delegate void MyEventHandler(GameObject sender, Collider col);
   public event MyEventHandler TriggerEnter;

  private Collider collider;

   private void OnTriggerEnter(Collider other)
   {
      collider = other;
    //call method to raise event
      TEnter();
     
   }

   public void TEnter()
   {
      MyEventHandler handler = TriggerEnter;
      //pass this and the colliding collider
      handler?.Invoke(gameObject, collider);
   }

}