I have two scripts. The first one plays a selected sound effect; the second calls a method in the first to play the required clip.
The first script is attached to an empty GameObject and the clips are added to it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaySound : MonoBehaviour
{
[SerializeField] AudioClip[] clips;
public AudioSource audioSource;
public float volume = 0.5f;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
public void PlayClip(int snd)
{
var clip = clips[snd];
audioSource.PlayOneShot(clip, volume);
}
}
The second script includes a declaration and a call to the PlayClip method:
public PlaySound sound; // Script containing PlayClip method.
...
sound.PlayClip(1);
I'm getting an error message to say that sound.PlayClip(1) needs an object reference, but I thought that was taken care of in PlaySound().
What am I not understanding?
Thanks.