Please help! Audio not working in another class..
Hello, I have a script called PlayerController which I attached it to my player, and in it I have an array of audio clips.
// audio
public AudioClip[] audioClip = new AudioClip[3];
private AudioClip sound;
And I have a getter which can access the specific audio clips inside the array.
public AudioClip getSound(int clip)
{
if(clip == 0)
{
sound = GetComponent<AudioSource>().clip = audioClip[0];
return sound;
}
else if(clip == 1)
{
sound = GetComponent<AudioSource>().clip = audioClip[1];
return sound;
}
else if(clip == 2)
{
sound = GetComponent<AudioSource>().clip = audioClip[2];
return sound;
}
return sound;
}
It works well when I use that getter inside my playercontroller class:
GetComponent<AudioSource>().clip = getSound(1);
GetComponent<AudioSource>().Play();
However, I cant access it from another class:
// audio
private PlayerController audio;
private AudioClip clip;
if(other.tag == "Player")
{
HealthManager.HurtPlayer(damageToGive);
//soundEffect.Play();
clip = audio.getSound(2);
GetComponent<AudioSource>().clip = clip;
GetComponent<AudioSource>().Play();
Debug.Log("play");
}
Why is that so?? Thanks for your help!
a.jpg
(10.3 kB)
Comment
Your answer
Follow this Question
Related Questions
Second AudioClip won't play 0 Answers
My sound effect is not playing 0 Answers
Play Audio Clips In Sequence From Array To Audio Sources In Array 0 Answers
Looping wav creates gap between plays 0 Answers
Second AudioClip won't play 0 Answers