- Home /
OnTriggerEnter through a different object
WHat I'm trying to do, is access one object's onTriggerEnter via the script on a second object. I've tried it as follows:
touchRange.OnTriggerEnter(Collider coll) {
if (coll.gameObject.name == "Player") {
playerCollision = true;
itemScript.curTouchedItem = itemName;
itemScript.amountOfItemsTouched++;
}
}
Where touchRange is an empty gameObject with a boxCollider (as trigger) that is a child of the object this script is attached to.
I hope it's not too vague ^^
Answer by Jessy · Dec 30, 2010 at 03:20 AM
You can't do what you want to do, directly. However, you can have OnTriggerEnter trigger an event, and have other classes listen for that event. This will yield better data encapsulation over what you are doing now.
Put this script on the game object with the collider:
public delegate void EventHandler (); public event EventHandler CollideWithPlayer;
// Checking a reference to a collider is better than using strings. [SerializeField] Collider playerCollider;
void OnTriggerEnter (Collider collider) { if (collider == playerCollider) CollideWithPlayer(); }
And on the parent object, you can use this:
bool playerCollision;
void Awake () {
touchRange.CollideWithPlayer += () => playerCollision = true;
}
itemScript.this and itemScript.that are not things you want to do. Instead, listen to the CollideWithPlayer event in ItemScript, directly, so things happening in ItemScript are more self-contained.
Yeah this is how I always do it to, but it is strange that there is no method to detect the trigger from a remote/abstract method.
There must be a way to do this as it would surely centralise code for triggers?
Answer by Headrush95 · Apr 12 at 10:37 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);
}
}
}