- Home /
Call function on all objects in trigger
C#
I want to call the function that is in the OnTriggerStay but it's only calling it for one object at a time. how can i make it so that it calls it for all the game objects at the same time? this will be used for an explosion-type damage.
using UnityEngine;
using System.Collections;
public class EnemyDamageZone : MonoBehaviour {
public float AmountOfDamage;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerStay (Collider ObjectToDamage){
if(ObjectToDamage.gameObject == GameObject.FindGameObjectWithTag("Enemy")){
ObjectToDamage.gameObject.GetComponent<ExperimentalEnemy> ().EnemyDamage (AmountOfDamage);
}
}
}
Answer by Wisearn · Oct 31, 2014 at 03:38 PM
When you detonate the explosion, you can put a single:
http://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html
That will give you an array of all objects in the sphere.
For purposes where you need a custom trigger shape, you would have to manually add objects to a list/array at OnTriggerEnter and remove objects from the list at OnTriggerExit and on detonation call the function on all the objects in the list but this can be a bit risky from my experience.
This is what I was trying to think of, but my brain only returned SphereCastAll. OverlapSphere gives less precise info (such as where the hit was), but you probably don't need to be that precise when doing an explosion. I like this answer better.
Does SphereCastAll return the closest hit point from origin?
That would be useful, more useful than just comparing this.transform.position to hitColliders[i].transform.position
But I imagine OverlapSphere being less harsh on performance
You can get hit.distance from each, but you'd still need to use $$anonymous$$athf.$$anonymous$$in to get the closest one.
the Unity Docs example works perfectly to the point where I do a Debug.Log (hitColliders[i]) but then when i add a line that says hitColliders.gameObject.GetComponent().nameOfTheFunction; and it gives me an error that it couldn't be found
You'd have to do a loop over hitColliders.Count/length and call nameOfTheFunction on each hitColliders[0], hitColliders[1] et.c.
Just like the example shows:
int i = 0;
while (i < hitColliders.Length) {
//here you go ins$$anonymous$$d of sendmessage
hitColliders[i].gameObject.GetComponent<Name>().FunctionName();
i++;
}
Answer by drod7425 · Oct 31, 2014 at 03:38 PM
You'll want to use Physics.SphereCastAll: http://docs.unity3d.com/ScriptReference/Physics.SphereCastAll.html
It returns every hit within a radius and you can use that to get the ExperimentalEnemy component and apply damage for each.
if it's an array, how would i call the function to all
You would loop through the returned array and call the function on each:
foreach(RayCastHit hit in Physics.SphereCastAll(etc...)){
hit.transform.gameObject.GetComponent<ExperimentalEnemy>().EnemyDamage (AmountOfDamage);
}
Your answer
Follow this Question
Related Questions
Want health to decrement longer stays on trigger 2 Answers
Work around for onTriggerStay? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers