- Home /
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!
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!
Your answer
Follow this Question
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