- Home /
c# destroy gameobject on 0 hp
i have a simple destroy script but i cant figure out how to change it from a key stroke destroy. insted i want when the enemyhealth hits 0 to destroy (and if possible only destroy the one in player area)
destroy script
using UnityEngine; using System.Collections;
public class EnemyDeath : MonoBehaviour { void Update () { if(Input.GetKey(KeyCode.Space)) { Destroy(gameObject); } } }
Answer by tw1st3d · Sep 26, 2013 at 03:00 PM
using UnityEngine;
using System.Collections;
public class EnemyDeath : MonoBehaviour
{
void Update()
{
if(Stats.health <= 0) // If stats class' health var <= 0, destroy enemy object
Destroy(this.gameObject);
}
}
public class Stats
// New class to store variables about your enemy
{
public int health; // Health variable
}
ive try this code and i get this error i know what it means but unsure what to do
script
using UnityEngine; using System.Collections;
public class EnemyDeath : $$anonymous$$onoBehaviour { void Update() { if(Stats.EnemyHealth <= 0) // If stats class' health var <= 0, destroy enemy object Destroy(this.gameObject); } }
public class Stats // New class to store variables about your enemy { public int EnemyHealth; // Health variable }
error
Assets/_Scripts/random scripts/EnemyDeath.cs(8,17): error CS0120: An object reference is required to access non-static member `Stats.EnemyHealth'
This isn't "working" code, it's an example of what you want to do. $$anonymous$$ake a new class for your enemy, and give it an HP value, then access it and when it's lower or equal to 0, destroy the object.
Also, if you want to just automatically use this, do
public static class Stats
and
public static int health
i tryed adding it dose not seem to work im prob adding it wronge if you can help
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Destroy Gameobject once 0 health 2 Answers
c# help fix argument is out of range error 1 Answer
how can i auto target Gameobject on startup 1 Answer
New to Unity, Need Help. 1 Answer