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 ThomasMarsh · Jan 29, 2015 at 12:40 AM · javascriptcollisioncollidertrigger

Using a Single Collider to Detect Collisions with Multiple Colliders

I am currently working on making a script for picking up tools. My current design is attempting to use a collider in the parent object of the player to detect collisions with three different types of tools and change booleans accordingly.

 function OnTriggerEnter (other : Collider){
     if(other.gameObject == pickaxe){
         canPickupPickaxe = true;
     }
     else if(other.gameObject == flareGun){
         canPickupFlareGun = true;
     }
     else if(other.gameObject == radMeter){
         canPickupRadMeter = true;
     }
 }
 
 function OnTriggerExit (other : Collider){
     if(other.gameObject == pickaxe){
         canPickupPickaxe = false;
     }
     else if(other.gameObject == flareGun){
         canPickupFlareGun = false;
     }
     else if(other.gameObject == radMeter){
         canPickupRadMeter = false;
     }
 }

However, when the player object collider collides with the tool's collider, the corresponding canPickup variable does not change. Is it possible to use a single collider to do this or have I made another mistake that is preventing this from working?

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

3 Replies

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

Answer by DanSuperGP · Jan 29, 2015 at 01:10 AM

  if(other.gameObject == pickaxe){

This comparison operation will only return true if the variable pickaxe references the exact same instance as the gameObject you have collided with. If pickaxe is the prefab, and the gameObject you collided with is an instance of that prefab, it will return false.

If the pickaxe has the tag pickaxe you should be checking.

 if(other.gameObject.tag == "pickaxe")

or, if you're doing it by name

 if(other.gameObject.name == "pickaxe")


Notice in both of those examples I am comparing the strings for the name or tag with another string. Comparing strings with the == operator will do a value comparison and return true if the strings have the same value.

What your code is doing with the == is comparing two references, which will only refer true if they refer to the exact same instance. That's almost certainly not what you want to do.

Comment
Add comment · Show 6 · 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 ThomasMarsh · Jan 29, 2015 at 01:15 AM 0
Share

That is true however I changed the code to using tags:

 function OnTriggerEnter (other : Collider){
     if(other.tag == "CollectPickaxe"){
         canPickupPickaxe = true;
     }
     else if(other.tag == "CollectFlareGun"){
         canPickupFlareGun = true;
     }
     else if(other.tag == "CollectRad$$anonymous$$eter"){
         canPickupRad$$anonymous$$eter = true;
     }
 }
 
 function OnTriggerExit (other : Collider){
     if(other.tag == "CollectPickaxe"){
         canPickupPickaxe = false;
     }
     else if(other.tag == "CollectFlareGun"){
         canPickupFlareGun = false;
     }
     else if(other.tag == "CollectRad$$anonymous$$eter"){
         canPickupRad$$anonymous$$eter = false;
     }
 }

And it still does not work.

avatar image ThomasMarsh · Jan 29, 2015 at 01:16 AM 0
Share

The way I am referencing the object does not seem to be the issue as I have tried tags, names and references and none have solved the issue.

avatar image DanSuperGP · Jan 29, 2015 at 01:20 AM 0
Share

Ok, this should be perfectly obvious... but the collider this is attached to is a trigger correct... and has a rigidbody attached, and the pickups also have rigidbodies attached.

avatar image DanSuperGP · Jan 29, 2015 at 01:25 AM 0
Share

Also, it's a good idea to add a debug.log to the beginning of your OnTriggerEnter. Something like

 Debug.Log("Trigger Entered : " + other.name);

That way you know that your OnTriggerEnter is actually happening, and it's not the comparison that is the problem.

avatar image ThomasMarsh · Jan 29, 2015 at 01:44 AM 0
Share

Yes it is a trigger however I have never needed to use rigidbodies before for pickup objects. I made a key using a similar method which works perfectly and it does not have a rigidbody. I also did try the debug line and it yielded no useful information.

Show more comments
avatar image
0

Answer by SnStarr · Jan 29, 2015 at 12:49 AM

try checking for the name of the game object for example:

instead of

 if(other.gameObject == pickaxe)
 {
 canPickupPickaxe = true;
 }

try:

  if(other.name == pickaxe)
 {
 canPickupPickaxe = true;
 }

because its already checking for the collider, just have it check for the name of the game object that the collider is attached to.

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 ThomasMarsh · Jan 29, 2015 at 12:53 AM 0
Share

I just tested this out and it had no change, as the game objects are already assigned using tags earlier in the script.

avatar image DanSuperGP · Jan 29, 2015 at 01:04 AM 0
Share

don't you mean

(other.name == "pickaxe")

What you wrote

if(other.name == pickaxe)

Is comparing a string to a game object reference, which will of course always return false.

avatar image
0

Answer by ThomasMarsh · Jan 29, 2015 at 01:56 AM

I managed to solve my own issue for this problem. I was using two different trigger colliders on the player object and the interaction between them was preventing my script from working properly. Thank you for the suggestions about referencing objects with triggers however.

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

21 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

Related Questions

Trigger Enter and Exit not working properly? 2 Answers

Collision with renderer.enabled? 0 Answers

Object Collision PLEASE HELP!!! 1 Answer

How do you execute Trigger-collider collision only in one gameobject? 1 Answer

Can a object without a rigidbody be triggered by a trigger? 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