- Home /
Zombie won't die
I have a zombie in my game. It can take damage but it won't die. Here is the script for the zombie.
using UnityEngine;
public class Target : MonoBehaviour { public static int Health = 100; public Animator anim; public GameObject Zombie;
public void TakeDamage(int amount)
{
void Update()
{
if (Health <= 0)
{
Die();
Invoke("Gone", 2);
Zombie.SetActive(false);
Debug.Log("the zomb died");
}
if (Health < 0)
{
Die();
Invoke("Gone", 2);
Zombie.SetActive(false);
Debug.Log("the zomb died");
}
}
void Die()
{
GetComponent<Animator>();
anim.Play("Death");
}
void Gone()
{
Destroy(gameObject);
Zombie.SetActive(false);
}
}
}
Have you checked if the health is decreasing? you should do Debug.Log(Health)
in the update(). This could be due to health not being reduced properly. Also within the update() you have unecessary conditions, you can delete the if (Health < 0)
, as it does the same thing as the first condition if (Health <= 0)
.
yes, I know that the health is decreasing. also, I will remove the unnecessary conditions
What do you mean by zombie wont die? do you meant script does not reduce health? because I don't see anywhere you actually subtracting any damage!, you should have something like Health = Health - Damage; and then check if health is less or equal zero..... or do you mean it wont play death animation? or that it wont remove zombie from the game?
and why is you Update function inside take damage function? Why do you use Destroy object and Set active.... you cant set inactive something that already has been destroyed
Ok 1. I do not mean it doesn't reduce health (I already have the health reducer in another script) 2. I also do not mean it won't play the death animation 3. I also do not mean that the zombie isn't being destroyed. 4. I don't know why the update function is in the takedamage function I, can change it The problem is that the death function is not being called. All of these things would happen but the if statement (if (Health <= 0) is not triggering.
Answer by jamesvhyde · Jul 27, 2020 at 05:57 PM
There is a problem in your originally posted code. All your methods ( Update
, Die
, Gone
) are nested inside another method, TakeDamage
. The TakeDamage
method shouldn't be there. Update
and the rest need to be inside the Target
class, not inside a method that is inside the class.
Also, this code is suspicious:
void Die()
{
GetComponent<Animator>();
anim.Play("Death");
}
You must mean either
void Die()
{
anim.Play("Death");
}
or
void Die()
{
GetComponent<Animator>().Play("Death");
}
The first one (and your code) will only work if you have animator set correctly in the Inspector in the editor.
Answer by rishitsood · Jul 27, 2020 at 05:20 PM
A screen recording would help`
file:///C:/Users/adam/Videos/Captures/Unity%202019.1.0f2%20Personal%20-%20[PREVIEW%20PACKAGES%20IN%20USE]%20-%20Apocalight.unity%20-%20scawy%20zombwi%20gwam%20he%20-%20PC,%20$$anonymous$$ac%20&%20Linux%20Standalone_%20_DX11_%202020-07-27%2014-12-00.mp4
if that doesnt work try this
Bruh, this is a link to a file on your computer. You cant send the link. You need to convert it to a format available to be shared.
oops sorry :/ as you can see I'm not very technically smart.
Your answer
Follow this Question
Related Questions
game object set active on trigger enter? 3 Answers
Problem with PlayerHealth and renderer 1 Answer
activate GameObject dont work 1 Answer
GameObject variable doesn't get filled in 0 Answers
Script is giving error that no MonoBehaviour scripts in file 0 Answers