- Home /
Multiple OnTriggerEnter commands in one script?
Hi,
Im using c# in order to make a large number of triggers (each with their own individual set of rules) activate if any one in particular are entered.
Also I want to put all of the "OnTriggerEnter" events into one script for ease of use.
The problem is I have written them each like the following numerous times as a list in my script.....
public void OnTriggerEnter(Collider cloth)
{
animation.Play("transparent word clip");
audio.Play("play sound");
print("you hit the cloth");
}
unity seems to not like this it says
"type "trigger_activation_script" already defines a member called OnTriggerEnter
with the same parameter types".
What I get from this is that unity is saying that I can only call one OnTriggerEnter event per script am I correct in saying this?
If not is there a way around this?
short answer: yes, you are correct. only one OnTriggerEnter-function per script. (Or rather per class to be more precise. But I'm not to good with the merits and pitfalls of multiple classes in one script...) anyway... I don't quite understand what you are trying to achieve exactly... could you explain some more? Greetz, $$anonymous$$y.
Answer by Bunny83 · Aug 31, 2011 at 06:04 PM
OnTrigger events are handled per GameObject. If you have multiple colliders on the same GameObject it will have only one OnTriggerEnter event for all those colliders. If you have multiple scripts attached to this GameObject all scripts will receive the same message.
Collider -\ /-> MonoBehaviour --> OnTriggerEnter
Collider --> GameObject ---> MonoBehaviour --> OnTriggerEnter
Collider -/ \-> MonoBehaviour --> OnTriggerEnter
Trigger should be a seperate GameObject but OnTrigger events are sent the that gameobjects so you need a script on this objects to receive the event and finally you can transmit the event to one managing script.
GameObject
{
ManagerScript
ChildGameObject1
{
Collider
TriggerScript
}
ChildGameObject2
{
Collider
TriggerScript
}
}
the scripts could look like that:
public class TriggerScript : MonoBehaviour
{
private ManagerScript m_Manager;
void Start()
{
m_Manager = transform.parent.GetComponent<ManagerScript>();
if (m_Manager == null)
{
Debug.LogWarning("Trigger: No manager script above - disabled");
enabled = false;
}
}
void OnTriggerEnter(Collider aOther)
{
m_Manager.OnChildTriggerEnter(name,aOther);
}
}
... and the ManagerScript:
public class ManagerScript : MonoBehaviour
{
public void OnChildTriggerEnter(string aName, Collider aOther)
{
Debug.Log(aOther.name+ " collided with " + aName);
}
}
Just a concept, not checked for syntax errors ;)
Thanks that's a really good idea im going to get to work on this straight away thanks again.
please don't post thank-yous as answers but as comments. but rather than thank-yous people around here like thumbs-ups and answer-accepts ;)
Answer by jtagamesinfo · Feb 18 at 07:07 PM
So if you mean you're looking to have different events trigger when different objects which will work as a pseudo different trigger functions this might be what you're looking for. You can also break down this concept into a hard coded script if you need to.
Your answer
![](https://koobas.hobune.stream/wayback/20220612195133im_/https://answers.unity.com/themes/thub/images/avi.jpg)