EASY How to code multiple tags if collision.
i want this gameobject to trigger if 2 or 3 tags are collided with it, I have this code which works for 1 tag but how do I do 2 or more tags in same line of code I think its by using the && bit of code
please guide me right thanks
2d game
if (other.gameObject.tag == "Player")
{
anim.Play("fishded");
anim.SetTrigger("Die");
Destroy (gameObject, 1.3f);
GetComponent<AudioSource> ().Play ();
}
}
}
so form what I think it would be
if (other.gameObject.tag == "Player" && "enemy")
or maybe if (other.gameObject.tag == "Player, enemy")
I just tried these but they don't work
I think it should be:
if(other.gameObject.tag == "Player" && other.gameObject.tag == "Enemy")
{
do();
}
List<string> tags;
tags = new List<string>{"Player","secondTag","thirdTag"};
if(tags.Contains(other.gameObject.tag)
{
// DO SOMETHING
}
this is a way I figured out to check for multiple tags in one line hope this method helps.
Answer by doublemax · Nov 28, 2016 at 08:15 AM
if (other.tag == "Player" && other.tag == "Enemy")
This condition can never be true, "tag" can only have one value at a time.
You need to keep a list of objects you're currently colliding with.
I see, yes I tried your code but it immediately gets red I the tag area and game wont play.
so how does one add multiple tags if you want to trigger against more than 1 tag? im confused?
thanks
I think what can work is if I use 2 if statements 1 if for each tag
if (other.tag == "Player"
if (other.tag =="Enemy")
this is prob not correct exactly but you get the idea
What excatly do you want to do?
Do you want a special behavior it your player "touches" multiple objects with different tags?
This is not possible with the information in the "other" class alone. You'd have a add a list to the script and keep track of all the objects you're currently colliding with.
Or do you just want to check against different tags and act differently depending on the tag?
In that case you can just use several if-clauses in a row:
if (other.gameObject.tag == "Player")
{
// do something here
}
else if (other.gameObject.tag == "Enemy")
{
// do something else
}
yes correct but I want both I have some players that will react different on collision and some same
so the code you gave me will work for separate reactions with that if statement
but for example I want the player if he collides with enemy 1 or enemy 2 it will trigger same action, but also I want (and I think u provided to me just now ) the other scenario of if player collides with enemy 3 or 4 enemy 3 has different action on collision and enemy 4 different from 3 as well
so its more like 2 scenarios I think u jus gave me solution to one of them already
As long as you need an "OR" condition, you can do it like this :
if (other.gameObject.tag == "Enemy1" || other.gameObject.tag == "Enemy2" )
{
// do something when we collide with "enemy1" OR "enemy2"
}
else if (other.gameObject.tag == "Enemy3" || other.gameObject.tag == "Enemy4" )
{
// do something else when we collide with "enemy3" OR "enemy4"
}
Answer by Yoshinator2 · Nov 27, 2016 at 06:53 PM
The easiest ways I could think of doing this are listed below.
if (other.tag == "Player" && other.tag == "Enemy")
{
anim.Play("fishded");
anim.SetTrigger("Die");
Destroy (gameObject, 1.3f);
GetComponent<AudioSource> ().Play ();
}
If this doesn't work there is a little messy alternative. As shown:
if (other.tag == "Player")
{
if(other.tag == "Enemy") {
anim.Play("fishded");
anim.SetTrigger("Die");
Destroy (gameObject, 1.3f);
GetComponent<AudioSource> ().Play ();
}
}
If this works, please accept the answer. If not, reply and I will help you further
Are you getting any type of error? Are the tags spelled correctly with the exact capitalization?
many errors
Assets/water/funcshark.cs(25,27): error CS1061: Type UnityEngine.Collision2D' does not contain a definition for
tag' and no extension method tag' of type
UnityEngine.Collision2D' could be found (are you missing a using directive or an assembly reference?)
Your answer
Follow this Question
Related Questions
Usage of multiple tags or some other workaround? 1 Answer
Need health with AI 1 Answer
find and print all gameobjects with a certain tag 0 Answers
2d touch not working 0 Answers