I have a script for an enemy that isn’t working. I don’t know if it’s because too many if statements or what. Any help would be great.
function OnCollisionEnter(collision : Collision)
{
if(collision.gameObject.tag == "Bullet")
{
skinTexture.renderer.material.mainTexture = hitTexture;
WaitTexture();
if(zombHealth == 1 && gameObject.animation == "Attack_1") //zombie is on player and one life left
{
CloseZombDeath(); // play close death animation
}
if(zombHealth == 1) // zombie has one life left
{
ZombDeath(); //play death animation
}
if(zombHealth == 3 && gameObject.animation == "Attack_1") //zombie is on player, don't make them run
{
zombHealth -=1; // take away one life from zombie
print(zombHealth);
}
if(zombHealth == 3) //zombie is walking towards player
{
zombHealth -=1; //take away one life
running = !running; // make zombie start running
print(zombHealth);
}
}
}
I think I explain what I am trying to do in the //notes.
I tried changing the last ifs to else-ifs and else, no results.
Almost any combination of ifs I could think of I tried.
I’m not sure if this helps but here it goes:
I’ve never dealt with the animation stuff before in unity, also I generally code in C#, but I worked with tags earlier, and if you are having a problem getting your main IF statement to fire, you may want to try using
if (collision.gameObject.GetComponent.CompareTag("Bullet")){
}
instead of:
if (collision.gameObject.tag == "Bullet"){
}
GetTag is actually a function that returns true if the Collider’s tag is the same as what you pass into the function.
Not sure if that is your problem, or even if it helps at all, but I hope it does.
Thanks all you guys for your responses. I tried all of them, and it didn’t work. But really, thanks for your advice.
What I ended up doing is making a boolean variable named “attacking” that would turn true when the zombie’s attacking animation started. Then I used that to compare in the “if” statement instead, seeing how it couldn’t check for the animation. And the reason why the life didn’t go down by 1 was because the zombies life started at 4, and there wasn’t anything saying to take one off from 4. So I added that “else” statement at the bottom:
function OnCollisionEnter(collision : Collision)
{
if(collision.gameObject.tag == "Bullet")
{
skinTexture.renderer.material.mainTexture = hitTexture;
WaitTexture();
if(zombHealth == 1 && attacking) //zombie is on player and one life left
{
CloseZombDeath(); // play close death animation
}
if(zombHealth == 1 && !attacking) // zombie has one life left
{
ZombDeath(); //play death animation
}
if(zombHealth == 3 && attacking) //zombie is on player, don't make them run
{
zombHealth -=1; // take away one life from zombie
Debug.Log(zombHealth);
}
if(zombHealth == 3) //zombie is walking towards player
{
zombHealth -=1; //take away one life
running = !running; // make zombie start running
Debug.Log(zombHealth);
}
else zombHealth -=1;
Debug.Log(zombHealth);
}
}