I've used two similar lines of code for two different scripts, but one works and the other doesn't. My slider is the one giving me the error code, but I don't see any differences from the other script.
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int startingHealth;
public int currentHealth;
public Slider healthSlider;
public AudioClip Hit;
public AudioClip death;
public GameObject Explosion;
AudioSource enemyAudio;
EnemyMove enemyMovement;
bool isDead;
void Awake()
{
enemyAudio = GetComponent();
enemyMovement = GetComponent();
currentHealth = startingHealth;
}
public void TakeDamageEnemy(int amount)
{
currentHealth -= amount;
healthSlider.value = currentHealth;
enemyAudio.clip = Hit;
enemyAudio.Play();
if(currentHealth <= 0 && !isDead)
{
Death();
}
}
void Death()
{
isDead = true;
enemyAudio.clip = death;
Instantiate(Explosion, transform.position, transform.rotation);
enemyAudio.Play();
enemyMovement.enabled = false;
Destroy(gameObject);
}
}
↧