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 Boownage · Sep 27, 2017 at 01:51 PM · unity 5collisiondestroy

How can I remove an object when colliding with an other object?

So, I am working on game in which you have to solve puzzles to go through the levels, the first level involves a color puzzle in which the player needs to pick 2 colors to combine them in the color needed. In my case the player needs to make the color purple. There are 4 blocks with different colors and the player needs to pick 2 of those blocks to make the color purple. The problem here is that I can't get it to work right, almost everything works but one thing. I need the blocks to dissapear when the player picks 2 colors but I can't get it to work, I have been trying for quiet some time but I just won't work.

Here is my code right now, it is different from what I want because here I am trying to just let the blocks stay but when you collide with a third block it will not give a color but a message that says you already have 2 colors. This is also not working for me.

 void OnTriggerEnter (Collider col)
     {
         if (col.gameObject.CompareTag ("Pick Up 1")) {
             red += 1;
             print ("Pick Up 1 collected");
             col.gameObject.SetActive (false);
         } else if (col.gameObject.CompareTag ("Pick Up 1") && blue > 0 && wrongPickup1 > 0) {
             twoColorsPicked.text = "You have already picked 2 colors";
             Destroy (twoColorsPicked, textTime);
         } else if (col.gameObject.CompareTag ("Pick Up 1") && blue > 0 && wrongPickup2 > 0) {
             twoColorsPicked.text = "You have already picked 2 colors";
             Destroy (twoColorsPicked, textTime);
         } else if (col.gameObject.CompareTag ("Pick Up 1") && wrongPickup1 > 0 && wrongPickup2 > 0) {
             twoColorsPicked.text = "You have already picked 2 colors";
             Destroy (twoColorsPicked, textTime);
         }


This is just the code for the first block, the code for the other blocks is very similar.

Thanks in advance!

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

1 Reply

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

Answer by wornTunic · Sep 27, 2017 at 03:37 PM

@Boownage In your first if statement, you check for gameObjects tag to be "Pick Up 1". If that is not the case, you go to your else statement, where you again check for that (among other things). None of the else cases will be executed, since all of them check for the same thing as first if, and the only way to reach them if that thing is not true. So, if you only want these things to happen for that tag, maybe this is the code for you:

 void OnTriggerEnter (Collider col)
      {
          if (col.gameObject.CompareTag ("Pick Up 1")) {

              if (blue > 0 && wrongPickup1 > 0) {
                  twoColorsPicked.text = "You have already picked 2 colors";
                  Destroy (twoColorsPicked, textTime);
              } else if (blue > 0 && wrongPickup2 > 0) {
                  twoColorsPicked.text = "You have already picked 2 colors";
                  Destroy (twoColorsPicked, textTime);
              } else if (wrongPickup1 > 0 && wrongPickup2 > 0) {
                  twoColorsPicked.text = "You have already picked 2 colors";
                  Destroy (twoColorsPicked, textTime);
              } else {
                  red += 1;
                  print ("Pick Up 1 collected");
                  col.gameObject.SetActive (false);
              }
     }

But I'm not sure, I don't know how your system works. Hope it helps!

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 Boownage · Sep 28, 2017 at 08:13 AM 0
Share

Thanks, this worked for me.

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

175 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to detect collision on only one side of an object? [C#] 4 Answers

How to only delete one of two collided objects? 1 Answer

Collision between 2 prefabs. 0 Answers

How do i prevent the player from dragging a ui element off screen? 1 Answer

Sphere (player) flattens/deforms when going on a moving platform. 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