I cannot get the sound to play for collectibles made from the following script:
public class SpawnerManager : MonoBehaviour
{
public GameObject prefab;
public int numberOfObjects;
public float radius;
public float heightTimesRadius;
void Start()
{
for (int i = 0; i < numberOfObjects; i++)
{
float angle = i * Mathf.PI * 2 / numberOfObjects;
Vector3 pos = new Vector3(Mathf.Cos(angle), heightTimesRadius, Mathf.Sin(angle)) * radius;
Instantiate(prefab, pos, Quaternion.identity);
}
}
This is my audio script, which I add as a component to my object along with the script above:
public class Collectable : MonoBehaviour
{
void OnTriggerEnter()
{
AudioSource source = GetComponent<AudioSource> ();
source.Play ();
}
}
I can use the 2nd script to make a random object I create trigger the sound on collision, but I cannot get the spawned objects from my first script to trigger any sound. Advice?
EDIT
It seems that the trigger is set to the coordinates in the center of the circle created rather than for each individual collectible that makes up the circle. Additionally, a sound plays immediately as I start the game (since I’m placed in the center of the circle of collectibles) despite having “play on awake” turned off in the audio source component.