• 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 MrDare360 · Nov 11, 2013 at 09:43 PM · colliderselevator

Multiple Trigger Colliders on One GameObject

I have two colliders on my elevator, one BoxCollider at the door and a CapsuleCollider on the inside of the elevator.

How do I check which Collider was triggered so I can execute different blocks of code?

This is my GameObject: Elevator

This is my code:

 using UnityEngine;
 using System;
 using System.Collections;
 
 public class Elevator : MonoBehaviour {
 
     public Transform elevator;
     public bool open = false;
     public bool verbose = false;
 
     private bool moving = false;
     private bool inElevator = false;
 
     public void Start() {
         
     }
 
     public void Update() {
         
     }
 
     public void OnTriggerEnter(Collider elevatorCollider) {
         if(elevatorCollider.GetType().ToString().Equals("BoxCollider")) {
             openDoors();
         }
     }
 
     public void OnTriggerExit(Collider elevatorCollider) {
         if(elevatorCollider.GetType().ToString().Equals("BoxCollider")) {
             closeDoors();
         }
     }
 
     public void OnTriggerStay(Collider elevatorCollider) {
         if(elevatorCollider.GetType().ToString().Equals("CapsuleCollider")) {
             Debug.Log("CC");
         }
     }
 
     private void openDoors() {
         if(!open && !moving) {
             elevator.animation["Default Take"].speed = 1;
             elevator.animation.Play("Default Take");
             open = true;
             if(verbose) Debug.Log("Elevator Opening");
         }
     }
 
     private void closeDoors() {
         if(open && !elevator.animation.IsPlaying("Default Take") && !moving) {
             elevator.animation["Default Take"].speed = -1;
             elevator.animation["Default Take"].time = elevator.animation["Default Take"].length;
             elevator.animation.Play("Default Take");
             open = false;
             if(verbose) Debug.Log("Elevator Closing");
         }
     }
 
 }


Comment
Add comment · Show 2
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 jacobschellenberg · Nov 11, 2013 at 09:57 PM 2
Share

$$anonymous$$ove the capsule collider to a separate GameObject and have it call to an ElevatorController or something similiar, essentially a central script in which the triggers report to when triggered, when its triggered. Unity doesn't like it when you have multiple colliders on a single object. You probably even got a little warning when you tried to attach the capsule collider.

avatar image aldonaletto · Nov 11, 2013 at 11:50 PM 0
Share

Only the other object can know which trigger it hit - the collider passed to OnTrigger events is the one that entered the trigger, thus you have no info about the trigger itself.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Getting multiple Polygon Collider 2D on script 2 Answers

physics.OverlapSphere colliders 1 Answer

Is there a way to increase the max number of colliders in the ParticleSystem.trigger module? 0 Answers

Animated characters does not stay upright with collider 1 Answer

Instantiating a worldspace UI on raycast hit .. am I doing it right? 0 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