when the Ai kills the player the game will freeze and the crash, here is the player health script;
public class Health :
{
public float health = 100f;
public GameObject deadPlayer;
public Transform player;
public TMP_Text healthtext;
void Update()
{
healthtext.SetText(health.ToString());
if (health <= 0)
{
Die();
}
}
void Die()
{
Destroy(gameObject);
Instantiate(deadPlayer, player.position, player.rotation);
}
}
You should probably swap Destroy and Instantiate, not totally sure if it will fix the issue tho. If you script destroying itself(or it’s gameObject) you should probably make it the last thing it does, because it won’t mean to exist afterwards. Also make sure all references in the script are filled. Also if you mean game build crashes, you should check how it goes in the editor. Here game don’t mean to crash, if something goes wrong it will by default just stop the game and throw an error in the console so you recognize and fix the issue.
Oh by the way, you should not do this
void Update()
{
healthtext.SetText(health.ToString());
if (health <= 0)
{
Die();
}
}
In the Update loop, you should update UI and check if player dead only after it’s got damage, not every frame. Just one check is not a big deal tho, but be careful at using ToString() every frame. Every time you do that it will generate a bit of garbage by creating a new string, not something you would want to happen dozens times per second.