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 Wereb · Dec 14, 2018 at 04:45 AM · collisioncollidercolliderscollision detectioncollide

How to detect collision between 2 objects while checking are they the ones that need to collide?

I've got 4 different color box colliders and 4 blue, red, yellow and green object colliders. I need to make it that when a blue object collider hits a blue box collider, it would give you points, if colors do not match, you would lose.

So any ideas how to detect collision between 2 very specific objects?

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 hexagonius · Dec 14, 2018 at 07:29 AM 0
Share

you have OnCollisionEnter which passes the other collider. so if yellow got that called and hit a red, it will contain red. you'd need to write the check there. problem is this method is always called on both participants. the easiest would be to give precedence, like yellow handles collisions with all others, red with blue and green and blue with green. green handles none on his side.
I assume the script will be one script for all.

avatar image hexagonius hexagonius · Dec 14, 2018 at 06:49 PM 0
Share

correction: Each box either handles only itself or the other box. That would also do.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kaosermail · Dec 14, 2018 at 08:28 AM

It's pretty simple. You need to have a script on each object with a function OnTriggerEnter(Collider other) and inside that function check if the colors are the same, or not. For example, for sprites it would be like this:

 void OnTriggerEnter(Collider other)
 {
     if (other.collider.GetComponent<SpriteRenderer>().color == gameObject.GetComponent<SpriteRenderer>().color) {
        // here you add points
      } else
      {
        // here you substract them
      }
 }

Notice that this will add or sub points twice, one for each script.

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 Wereb · Dec 15, 2018 at 05:11 PM 0
Share

Thanks for the reply, I'm quite new to this, do you need to tick the "Is Trigger" box on, at the collider component in the inspector pannel? If so, where? On the object which should have the script or on the object which owns (Collider other)? I'm using UI, with a canvas so I'm not using sprite renderer. $$anonymous$$y code is:

void OnTriggerEnter(Collider other) {

     Debug.Log("Collided");

     if (other.gameObject.name == "GameObjectR")
     {

         Destroy(other.gameObject);
         spawnBalls.score++;

     }
     else
     {

         Destroy(other.gameObject);
         // load another scene

     }

 }

But It doesn't do anything, it doesn't even display the message on console.

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

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

How do i understand which gameObject is player touching ? 1 Answer

How to get OnTriggerEnter effect without using isTriggered on Collider? 1 Answer

Why there is no Collider.IsTouching(...) ? 2 Answers

Inconsistent Collision Detection 3 Answers

Strange issue with collider 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