Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 hfmiguel · May 23, 2018 at 10:07 PM · collidersontriggerenterif-statements

OntriggerEnter condition with two colliders

Hello,unity beginner here. i'm making a "escape room" game where you have to find the 5 numbers across the room to be able to leave it. One of the numbers appears when the player finds all the chairs and put them with the table, problem is, i don't know how to use the OntriggerEnter with more than one object. I don't know if i'm explaining myself, with the code i think it will become clear. I have this:

     public GameObject handNumber;
 
     void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Chair")
         {
 
             handNumber.SetActive(true);
         }
         
     }
 }


That works ok, but i have multiple chairs so it would be something like:

     public GameObject handNumber;
 
     void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Chair" && "Chair2")
         {
 
             handNumber.SetActive(true);
         }
         
     }
 }


But that doesn't work. Could you help me? Thank you so much!

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
1
Best Answer

Answer by winterfluxstudio · May 23, 2018 at 10:39 PM

Whenever you work with stuff like this, it's always best to temporarily store the information you want to compare something with/against.

1) every time a chair is placed in the designated area, increase an integer count 2) add a "chair_X_has_been_triggered" or something to ensure trigger only activates once 3) when the integer total is X - do something

 public GameObject handNumber;
 
 // has Chair 1 been placed near table? etc
 public bool Chair1Placed;
 public bool Chair2Placed;
 public bool Chair3Placed;
 
 // how many chairs have been placed?
 public int TotalChairsPlaced = 0;
 
 void onTriggerEnter(Collider other)
 {
     // if Chair1 is placed in trigger box 
     // and it hasn't been placed here before
     // increase the Total Chair count by 1
     if (other.tag == "Chair" && Chair1Placed == false)
     {
         TotalChairsPlaced += 1;
         Chair1Placed = true;
     }
 
     if (other.tag == "Chair2" && Chair2Placed == false)
     {
         TotalChairsPlaced += 1;
         Chair2Placed = true;
     }
 
     if (other.tag == "Chair3" && Chair3Placed == false)
     {
         TotalChairsPlaced += 1;
         Chair3Placed = true;
     }
 }
   
 void Update()
 {
     if (TotalChairsPlaced == 5)
     {
         handNumber.SetActive(true);
     }
 }
 

Comment
Add comment · Show 1 · 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 hfmiguel · May 23, 2018 at 11:06 PM 0
Share

Thanks man! That worked like a charm =)

avatar image
-1

Answer by TanselAltinel · May 23, 2018 at 10:41 PM

Hi,

When you are programming with C# in Unity, you are actually writing C# code, with its all functionality.

In the if statement in your code, you are making a String comparison, checking if the collider entered into trigger area has the same tag as "Chair".

You can also use CompareTag("your game object tag") function to compare this.

Also, && is actually means that if and if, but where a game object cannot have more than one tag, so I assume you want to check if or if, using ||

So your code becomes:

  public GameObject handNumber;
  
 void OnTriggerEnter( Collider other) {
     if ( other.CompareTag("Chair") || other.CompareTag("Chair2") ) {
         handNumber.SetActive(true);
     }
 }


Mind that you are checking for game object tags. You can also compare for game object names.

Comment
Add comment · Show 9 · 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 winterfluxstudio · May 23, 2018 at 10:53 PM -1
Share

That will not work as it will enable the handNumber GameObject if any of the chairs are placed within the trigger. OP only wants to activate the GO when a certain number of chairs have been placed (for example, when 5 chairs are placed then handNumber.SetActive(true)

 One of the numbers appears when the player finds all the chairs and put them with the table

avatar image TanselAltinel winterfluxstudio · May 24, 2018 at 08:31 AM -1
Share

"Work" as in won't work for the specific case asked. I thought it was better to show direction and let the op improve himself, but well.

avatar image winterfluxstudio TanselAltinel · May 25, 2018 at 06:17 PM 0
Share

It's called Unity Answers... not Unity Show Direction and Let the OP Improve Himself. I assume thats why you downvoted my answer even though it was correct. That's petty for someone with +1000 rep.

Show more comments

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

86 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 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 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 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

Can't call GetKey inside OnTriggerEnter? 2 Answers

OnTriggerEnter/Exit not working 1 Answer

When player enters triggerzone call function 2 Answers

Application.LoadLevel ("lower") loads on start-up, not when triggered; Deadline is soon HELP! 3 Answers

Trigger in child object calls OnTriggerEnter in parent object 3 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