- Home /
how to detect OnTriggerEnter on ANOTHER gameObject
How to detect OnTriggerEnter on ANOTHER gameObject? I know how to detect such event on the object where the script itself is attached. In my mini-game I would like to use a script on my main object and from there detect a collision/triggerEnter on another gameObject. Is that even possible?
I don't believe so. I think what I would do would be to attatch a script to the other game object and on startup get a reference to your main object's script. Then in that other object's ontriggerenter, just call a function on your main object's script. That way, your main object's script can get a notification when the other object's script detects a collision.
@Intervention I'm aware of that, in fact, that is how I have it set up at the moment. What bothers me is, that I have a lot of sub-objects that need collision detection and thus they have to have the same collision detection script attached to them. I wanted to have one script on my main object and iterate through the sub-objects when detecting collision. Not that I wouldn't trust you :) but I want to be absolutely sure - I'll wait for a couple of days for some more responses to my question and we'll see.
Answer by TimCoster · Mar 22, 2021 at 09:44 AM
Hey future me and other me's, you can use those unity events!
On the other GameObject:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class OnTriggerStayEvent : MonoBehaviour
{
public UnityEvent<Collider> onTriggerEnter;
void OnTriggerEnter(Collider col)
{
if(onTriggerEnter != null) onTriggerEnter.Invoke(col);
}
}
On this GameObject:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Example : MonoBehaviour
{
public OnTriggerEnterEvent onTriggerEnterEvent;
void OnEnable()
{
onTriggerEnterEvent.onTriggerEnter.AddListener(OnTheOtherTriggerEnterMethod);
}
void OnDisable()
{
onTriggerEnterEvent.onTriggerEnter.RemoveListener(OnTheOtherTriggerEnterMethod);
}
void OnTheOtherTriggerEnterMethod(Collider col)
{
if(col.gameObject.tag == "Enemy")
Debug.Log("Enemy entered the trigger!");
}
}
shouldn't your second code block use "onTriggerStayEvent" instead of "onTriggerEnterEvent" to match the first code block?
Answer by tanoshimi · Jan 03, 2015 at 10:37 AM
You can't. OnTriggerEnter()
messages are sent to the trigger collider and the rigidbody (or the collider if there is no rigidbody) that touches the trigger only - you can't subscribe to them from a 3rd party script. http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html
You can of course create a global collision manager, but each object will have to be responsible for updating it with their own collisions. Assuming all your sub-objects are created from the same prefab, or derived from the same parent class, you simply attach your collision script there.
Answer by Headrush95 · Apr 12 at 09:11 PM
for those who still need this you can do this.
on the parent (weapon holder) script you can create a method like this. the method will take a collider and then we can assign it to anything.
public class WeaponsController : MonoBehaviour
{
//we would drag and drop the weapon in the inspector
public GameObject weapon;
private float Damage;
private Collider EnemyCollider;
private bool enemyHit;
public void Start()
{
//on our weapon we may have a script called "Sword" that contains all of the sword
//information, such as "Damage"
Damage = weapon.GetComponent<Sword>().Damage;
}
public void EnemyHit(Collider other)
{
EnemyCollider = other;
//if we have a health script on our enemy we may want to damage the enemy
//so lets say we have a health script
//we could call the take damage function on the enemy
//that was hit
EnemyCollider.GameObject.GetComponent<Health>().TakeDamage(Damage);
Debug.Log("Enemy Hit");
enemyHit = true;
// here we can take the collider that our weapon has sent us and we can assign
// it to a new variable so that we can make any changes.
}
}
then on the weapon you can create a script and do this. you can check for collision and then call the function from your parent script and use the collider that was detected as the collider in the method you created.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponCollisionDetection : MonoBehaviour
{
public WeaponsController WC;
public void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Enemy")
{
WC.EnemyHit(other);
}
}
}
this will allow you to grab what ever your weapon hit and then do what ever to it, such as damage it.
Your answer
