Hi. I’m having some problems with playing sounds on prefabs. I’m throwing in an AudioClip to the script that I simply drag with the inspector to assign. Then I’m trying to play that clip with the AudioSource but I’m getting the NullReferenceException whenever I’m doing anything with that AudioSource. Needless to say Audio doesn’t play.
Here’s the code:
using UnityEngine;
using System.Collections;
public class Footsteps : MonoBehaviour {
public AudioClip footsteps;
private AudioSource footstepsSrc;
void Start () {
footstepsSrc = new AudioSource();
footstepsSrc.clip = footsteps;
}
void Update () {
if(GetComponent<Movement>().IsMoving())
{
if(!footstepsSrc.isPlaying)
footstepsSrc.Play();
}
else
{
if(footstepsSrc.isPlaying)
footstepsSrc.Stop();
}
}
}
Works like a charm! Thanks!
– waxx