I have setup a script to play a sound from the audio source attached to the gameobject when it is collided with and works perfectly everytime.
Problem When I save and close unity then reopen the project, the sound no longer works when triggered. The same happens when switching scenes.
There are no errors in my code.
public Material noiseTexture;
public Material scareTexture;
public AudioSource scareSound;
public Renderer rend;
public float scareTime = 3f;
void Start()
{
scareSound = GameObject.FindObjectOfType<AudioSource>();
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
StartCoroutine("Scare");
}
}
public IEnumerator Scare()
{
scareSound.enabled = true;
scareSound.Play();
Debug.Log("played sound");
rend.material = scareTexture;
yield return new WaitForSeconds(scareTime);
scareSound.Stop();
rend.material = noiseTexture;
}
}