Other
Why ain't this working?
I'm trying to run the collision against the tags to make how much damage they take change depending on the tag. like if hit by enemy ship 25 health, bullet 1 health and ect.
here the code I pulled it off a help site but it not working.
void OnTriggerEnter2D(Collider other){ // here it checks for triggers
if (other.gameObject.tag == "enemy") // here I want it to check if the other object that it touching is enemy, enemy bullet, boss lazar ect;
{health -= 25;} // here it loses health
}
I'm using C#, Unity 5. Can anyone give me any ideas of how to get this to work. yes i gave the enemy his tag.
It says error onTriggerEnter2D, but nothing else;
Answer by Jawchewa · Jun 13, 2017 at 11:59 PM
My guess would be that you are missing your rigidbody. To get the OnTriggerEnter2D function to trigger, at least one of your game objects needs to have a Rigidbody2D component in order for the function to be called. Although, it could still be something else causing the issue.
all $$anonymous$$e got rigidbody2d and boxcolider2d. that like the first thing i do when creating a game like this.
Actually, I think I might see the issue. The parameter for OnTriggerEnter2D has to be the type Collider2D. You have it just as a normal Collider. Change it to:
void OnTriggerEnter2D(Collider2D other){
it worked is there a way to turn it into like an array or list ins$$anonymous$$d of using if tag = this and that.