i want to be able to add bird sounds but i just dont know how, i have a code that turns the lights on at night and off in the morning and i want to use the same way to play the sound effects at specific time.
Hello, there are many different ways to get there, depending on how high quality you want the whole thing to be.
The quickest and easiest solution would be to place an AudioSource Component directly on top of your object that turns the lights on and off and switch the AudioClip for it.
I have a similar little snippet of code, which isn’t much more complex either, but could at least be extended:
public enum Ambient { Day, Night }
public Ambient ambient;
public AudioClip dayAudioClip;
public AudioClip nightAudioClip;
private AudioSource audioSource;
private void Awake()
{
audioSource = GetComponent<AudioSource>();
SetAmbient(ambient);
}
public void SetAmbient(Ambient wantedAmbient)
{
ambient = wantedAmbient;
if (ambient == Ambient.Day && dayAudioClip) audioSource.clip = dayAudioClip;
if (ambient == Ambient.Night && nightAudioClip) audioSource.clip = nightAudioClip;
audioSource.Play();
}
Just drag the script onto a GameObject, assign the AudioClips and try it out.
If you don’t know how to access it from another script, just let me know in the comments. From another script on the same GameObject, if we assume the name of your class is AmbientManager, for example it would be:
GetComponent<AmbientManager>().SetAmbient(AmbientManager.Ambient.Night);
Of course you could do a lot of cooler things like fade each clip in and out, but I think that should do the job for starters.