Hello people I am just a student at this so please be kind.
I have been searching for 2 days now just can’t seem to find the answer I am looking for.
I have a game where if the player kills the other enemy player, I want it to trigger an explosion sound when the enemy player’s health reaches 0.
so I have 2 scripts
player2.cs
and
AudioScript.cs
player 2 has a health variable
public int enemyhealth = 100;
and in that script is a function to decrease health on hit.
if (enemyhealth < 0)
{
Destroy(this.gameObject);
}
and that all works fine.
but in AudioScript.cs is where my problem lies.
public class AudioScript : MonoBehaviour {
//Oneshot Explode Sound
public AudioClip ExplodeSound;
private AudioSource source;
private float volLowRange = .5f;
private float volHighRange = 1.5f;
//Reference to player2 Script
public player2 healthRef;
//reference to UI Manager
public InGameUIManager UIManager;
void Awake()
{
source = GetComponent<AudioSource>();
}
void Start()
{
healthRef = GetComponent<player2>();
}
void Update()
{
if (healthRef.enemyhealth < 0)
{
explodeSound();
}
}
void explodeSound()
{
float vol = Random.Range(volLowRange, volHighRange);
source.PlayOneShot(ExplodeSound, vol);
}
}
I make a sound emmitter object, I attach AudioScript.cs to it, I attach the explosion to the script, I add an audiosource and link the sound, I link the healthRef variable to player2 in Unity.
but when I run unity I keep getting a red error on each frame.
when I double click the error its complaining about this line in AudioScript.cs
if (healthRef.enemyhealth < 0)
its like its not linking to the other script correctly or something I been trying everything I can find to make it get past this error I feel I’m really close but I am missing something.
Any help is greatly appreciated.