Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by melonman · Aug 31, 2011 at 04:09 PM · triggersmultiple

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SisterKy · Aug 31, 2011 at 05:50 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
8

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 ;)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image melonman · Sep 01, 2011 at 12:07 PM 0
Share

Thanks that's a really good idea im going to get to work on this straight away thanks again.

avatar image SisterKy · Sep 01, 2011 at 12:30 PM 2
Share

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 ;)

avatar image
0

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.

https://www.youtube.com/watch?v=3ksdgZDClhM

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using multiple triggers as one trigger zone 1 Answer

If multiple triggers activated 1 Answer

Switching Several Cameras(HorrorGame) 1 Answer

Multiple triggers on one object 8 Answers

Two trigger activation. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges