Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Razputin · Feb 26, 2014 at 06:03 AM · collidertriggerfalsetrue

How to do Multiple OnTriggerEnters

I've seen a couple of old posts that ask this question but i'm having trouble understanding the answers people have given. Can anyone explain it to me in an easy to understand way?

here is the code i'm trying to use. I need to return which tile the player is standing on (In this case the grasslandTile) and change the true/false for the other tiles.

     void OnTriggerEnter(Collider grasslandTile){
             enterGrassland = true;
             enterHouseWithTree = false;
             enterSingleTree = false;
         }

But I also need one thats like

 void OnTriggerEnter(Collider houseWithTreeTile){
             enterGrassland = false;
             enterHouseWithTree = true;
             enterSingleTree = false;
         }
 

and then

   void OnTriggerEnter(Collider singleTreeTile){
                     enterGrassland = false;
                     enterHouseWithTree = false;
                     enterSingleTree = true;
                 }
         
 
 

So how do I get these all into one script? I need them all in one script to return the value for this

 void OnGUI(){
         if(guiOn && enterGrassland == true)
         { specific 
                 GUI.Button(new Rect(10,200,1200,100), "Forage");
                 GUI.Button(new Rect(10,400,1200,100), "Rest");
                 GUI.Button(new Rect(10,600,1200,100), "Wait");
         }    
 if(guiOn && enterHouseWithTree== true)
             { specific 
                     GUI.Button(new Rect(10,200,1200,100), "Search");
                     GUI.Button(new Rect(10,400,1200,100), "Rest");
                     GUI.Button(new Rect(10,600,1200,100), "Wait");
             }    
         }
     }
     
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by GambinoInd · Feb 26, 2014 at 06:06 AM

There can only be one OnTriggerEnter and it will be called no matter what name the collider has or what you name the collider variable in the method. What you need to do is within the method, check for the gameobject's name.

For example:

 void OnTriggerEnter(Collider collider){
              if(collider.gameObject.name == "Tree Tile") {
                enterGrassland = false;
                enterHouseWithTree = false;
                enterSingleTree = true;
              } else if(collider.gameObject.name == "Grass Tile") {
                //Do stuff.
              }
             }


You can put them all in one script, by simply putting them all in one script! I don't quite understand the issue with that part.

Comment
Add comment · Show 3 · 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 Razputin · Feb 26, 2014 at 06:11 AM 0
Share

I see thank you that was much easier to understand. So something like this?

     void OnTriggerEnter(Collider collider){
         if(collider.gameObject.name == "grassLandTile")
         {
             enterGrassland = true;
             enterHouseWithTree = false;
             enterSingleTree = false;
         }
         else if(collider.gameObject.name == "houseWithTreeTile") 
         {
             enterGrassland = false;
             enterHouseWithTree = true;
             enterSingleTree = false;
 
         }
         else(collider.gameObject.name == "singleTreeTile") 
         {
             enterGrassland = false;
             enterHouseWithTree = false;
             enterSingleTree = true;
         }
     }
 
avatar image GambinoInd · Feb 26, 2014 at 06:14 AM 1
Share

Yep that should work. Just don't forget to add the if to the last else if in the code you posted.

avatar image Razputin · Feb 26, 2014 at 06:22 AM 1
Share

After a few typo fixes got it working perfectly ;) thanks for making it so easy to understand!

avatar image
4

Answer by justinpatterson · Feb 26, 2014 at 06:12 AM

Basically what you want to do is

(1) add tags to or give your objects individual names that the player can stand on

(2) In your code have a switch statement that looks at the tag/name of the thing it collided with.

  void OnTriggerEnter(Collider singleTreeTile){
      switch(singleTreeTile.gameObject.name){ // or singleTreeTile.gameObject.tag
           case "grassland":
                    // true false false
                    break;
          case "houseWithTree":
                    // false true false
                    break;
          case "houseWithTree":
                   //false false true
                    break;
          }
 }
Comment
Add comment · Show 4 · 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 GambinoInd · Feb 26, 2014 at 06:22 AM 1
Share

Tags are another option, good idea justin. I recommend using switches as well, I just didn't use them in my answer to keep it simple to understand.

avatar image Aethenosity · Jul 01, 2018 at 12:31 AM 0
Share

In this specific example, with only three cases, an if else chain would be faster by single digit milliseconds. It's only at about 5 or more that you actually start seeing better efficiency. Or at least that's what I've read.

avatar image justinpatterson · Jul 01, 2018 at 01:48 AM 0
Share

That's true, Aethonosity. I prefer switches for readability, but the primary point of the post was to emphasize tags over names for the sake of control. I just hate typing out if statements and needing to write "==" a thousand times and whatnot heh. The switch statement, while I admit emboldening it was a bit much, was the secondary point.

We're basically at a point in game dev where time saved by using a switch statement are insignificant in comparison to just good program$$anonymous$$g practices elsewhere. I know this post is pretty dang old, but I figured I'd add my two cents to your comment. Thanks for inviting a discussion on the differences between ifs and switches for new programmers finding this post.

avatar image Aethenosity justinpatterson · Jul 01, 2018 at 06:31 AM 0
Share

That all makes a lot of sense. I appreciate your response! I'm a pretty new programmer, so I'm definitely still learning where it's important to optimize and where it's just an unnecessary headache. Haha

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

23 People are following this question.

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

Related Questions

I can't figure out how to enable / disable a trigger. 2 Answers

Boolean problem!! 2 Answers

Can't click gameobject when over another trigger? 1 Answer

The public bool will not change unless i set it 1 Answer

Physics Possibilities?: Angles, Penetration, Exceptions 4 Answers


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