- Home /
OnTriggerStay for many object
Hi. I'm looking for a solution to my problem: I have in the scene object with collider and rigitbody and that obejct collide with the other one. I'm using function OnTriggerStay and i want to change material of every object which is touching my main object. How i can make it work not only for one object at the time but for all?
ex. script
void OnTriggerEnter (Collider col)
{
col.renderer.material = someMaterial;
}
Answer by betaFlux · Sep 13, 2014 at 07:18 AM
You could store all the objects in a List or Array, loop through them and compare them in the OnTriggerStay function:
public List<GameObject> listOfObjects; // needs using directive 'System.Collections.Generic'
void Start()
{
listOfObjects = new List<GameObject>();
listOfObjects.AddRange(GameObject.FindGameObjectsWithTag(Choose a tag));
}
void OnTriggerStay(Collider c)
{
foreach(GameObject go in listOfObjects)
{
if(go.transform == c.transform)
{
go.renderer.material = someMaterial;
}
}
}
Answer by IntergalacticSloth · Feb 27, 2018 at 01:40 AM
Not sure. But I think in that first line, variable definition could instead read:
public List<GameObject> listOfObjects;
No brackets that first time.
Your answer
Follow this Question
Related Questions
OnTriggerStay/Enter/Exit 2 Answers
OnTrigger event when colliders are already touching eachother 1 Answer
OnTriggerExit is not calling 2 Answers
OnTrigger* triggered only once 1 Answer
Have "OnTriggerStay" detection but only call it once? 1 Answer