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 Spelix95 · Jul 08, 2016 at 11:32 AM · bugcolliderstriggers

Problem with multiple colliders in a trigger

I'm making a game where a character can pick up rocks and throw them at wood and then pick them up again.

The issue I'm having comes when trying to pick up a rock that lies right in front of the wood. The thing is that both the rock's and the wood's colliders are triggered when the players OnTriggerStay2D is run. The player only tries to pick up the wood and ignores the rock completely, even though they are both inside the trigger for the player.

alt text

The smaller rectangle right in front of the player is his trigger to pick things up, the brown block is the wood with a box collider and the round grey thing is a rock with a polygon collider. This code is attached to the player:

     void OnTriggerStay2D(Collider2D col) {
         if(Input.GetButton ("Pickup") && !holdPickup && !charInventory.isHoldingItem()) {
             Debug.Log (col.gameObject);
             holdPickup = true;
             switch(col.gameObject.tag) {
 
             case "rock" :
                 charInventory.setHoldingItem(col.gameObject);
                 col.gameObject.GetComponent<PickUpableItem>().PickedUp (this.gameObject);
                 break;
             }
         }
     }
 }

The debug-log only prints out "wood", which means that the method is only triggered on the wood- collider and ignores the stone. I have paused in-game and checked the range, but they are both in range for the players trigger.

Why is that? Why won't the method sometimes check for the rock?

Thanks!

buggfixhb.jpg (86.0 kB)
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

Answer by jalal_moghaddam · Apr 06, 2017 at 03:32 PM

hi, i had same problem and solve it by making a trigger list

 void OnTriggerEnter2D (Collider2D objectInTrigger) 
     {
         triggerList.Add(objectInTrigger.name);
     }
 
 void OnTriggerExit2D(Collider2D objectExitTrigger)
     {
         triggerList.Remove (objectExitTrigger.name);
     }
 
 public void DoAction()
     {
         foreach (string triggerObjectName in triggerList) 
         {
             switch (triggerObjectName) {
             case "LightKey":
                 lightKeyEventScript.DoAction ();
                 break;
 
             case "Toilet":
                 toiletEventScript.DoAction ();                
                 break;
             }
         }
     }
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
avatar image
0

Answer by shadowpuppet · Apr 05, 2017 at 07:32 PM

give the rock a tag "rock" and add another condition

     void OnTriggerStay2D(Collider2D other) {
 if(other.tag == "rock")
 {
              if(Input.GetButton ("Pickup") && !holdPickup && !charInventory.isHoldingItem()) {




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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

When player enters triggerzone call function 2 Answers

Trigger in child object calls OnTriggerEnter in parent object 3 Answers

Does OnTriggerStay ignore layers? 1 Answer

How do I create AI with a good side and evil side? 0 Answers

get only one colliding body 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