- Home /
I want this script to destroy the enemy
using UnityEngine;
using System.Collections;
public class death : MonoBehaviour {
Transform curTransform;
void Start(){
curTransform = gameObject.GetComponent<Transform>();
curTransform = gameObject.transform;
}
void Update(){
EnemyHealth eh = gameObject.GetComponent<EnemyHealth>();
eh.GetComponent<health>();
eh.health = 0;
}
GameObject Wolf_run;
void Updatetwo(){
Destroy(gameObject);
}
}
the script does not do anything when the enemy reaches 0 health I want the enemy to be destroyed when it reaches zero health. im very new to coding
Answer by Fornoreason1000 · May 22, 2013 at 09:15 AM
okay if your new I see no harm in telling you what your doing wrong.
you getting a component of a component, after you assign Enemy health just use eh.health, no need for this line
eh.GetComponent();
you set the enemy health every frame(Update runs every frame) this should really be
if(eh.health == 0)
UpdateTwo(its not like update, you have to call it manually) never gets called, i would call it right after
if(eh.health == 0)
You double assign CurTransform delete this line
curTransform = gameObject.GetComponent();
you also declare a useless variable called "wolfRun" Scrap it.
that should just about cover it.....hope it helps. also check some C# tutorials by
thank you it works and thanks for the link to the tutorials too
Your answer
Follow this Question
Related Questions
What's wrong with this code? 2 Answers
Environment Interaction 1 Answer
make my jump anim complete before walk anim starts? 2 Answers