- Home /
c# destroy Gameobject on 0 hp
i have three monsters there skeleton clones when the game starts up i only want 1 to die if the hp drops to zero not all of them die cause they have the same name i have my targeting script witch i can target the closest enemy in the game to the player and so on it has a
here's my targeting script
/// /// This script can be attached to any permanent gameobject, and is responsible for allowing the player to target different mobs that are with in range /// using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Targetting : MonoBehaviour { public List targets; public Transform selectedTarget;
private Transform myTransform;
// Use this for initialization
void Start () {
targets = new List<Transform>();
selectedTarget = null;
myTransform = transform;
AddAllEnemies();
}
public void AddAllEnemies() {
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in go)
AddTarget(enemy.transform);
}
public void AddTarget(Transform enemy) {
targets.Add(enemy);
}
private void SortTargetsByDistance() {
targets.Sort(delegate(Transform t1, Transform t2) {
return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
});
}
//if we do not have an enemy targeted ywt, then find the clostest one and target him
//if we do have an enemy targeted, then get the next target
//if we have the last target in the list, then get then first target in the list
private void TargetEnemy() {
if(selectedTarget == null) {
SortTargetsByDistance();
selectedTarget = targets[0];
}
else {
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count - 1) {
index++;
}
else {
index = 0;
}
DeselectTarget();
selectedTarget = targets[index];
}
SelectTarget();
}
private void SelectTarget() {
selectedTarget.renderer.material.color = Color.red;
PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
pa.target = selectedTarget.gameObject;
}
private void DeselectTarget() {
selectedTarget.renderer.material.color = Color.white;
selectedTarget = null;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Tab))
TargetEnemy();
}
}
Answer by charlesvi · Sep 25, 2013 at 09:05 AM
I'm not really sure what part of that code you think is going to kill your guy. Also it looks more like a game loop than something I would put on my bad guys.
So to kill your dude your on the right track.
You have an array of GameObjects. Thats a good start. Can you make a class?
Something simple like this:
SkeleyClass
{
GameObject go;
int hitPoints = 100;
public ModifyHP(int damage)
{
hp -=damage;
}
then from your main when your skelly takes damage find it by location or w/e lets say you use the collider on the model so it looks like:
hit.collider.gameobject
then you find your dude in the dude array
foreach(GameObject go in dudearray[]) { if go == hit.collider.gameObject { go.ModifyHP(damagedelt) //Is it dead? if(go.hp <= 0) { go.enabled = false; } }
}
should work there are other ways too. give it a try if it doesnt work I'll try to tell you why. Your on the right track. Sorry the formatting is shit.