[SOLVED] Trying to add sound effects and background sprites

Hi there,
I just finished the TEXT101 game but found it too “empty” with text only, I tought about implementing some background sounds and some scene sounds(on key down) but just don’t know how (background sprites changing with the scene would be great too).
Can anybody point me at the right direction?

You’ll need AudioSource attached to a game object

public AudioSource jukebox;
public AudioClip soundYouWantToPlay;

for sound effects I use

jukebox.PlayOneShot(soundYouWantToPlay, 1)
the PlayOneShot method takes in two arguments, the jukebox.PlayOneShot(AudioClip, volume)

for Soundtracks and background noises I just have it play on awake unless I need to stop or change the sounds.

This is a critical part about sounds.

MAKE SURE THE SOUND PLAYS ONCE!,

if you have something like
if(hasMirror == true)
{
jukebox.PlayOneShot(soundYouWantToPlay, 1)
}
The sound will play more than twenty times per second and with all those audio tracks playing at once it sounds dreadful.

even
Input.GetKeyDown(KeyCode.m)){
jukebox.PlayOneShot(soundYouWantToPlay, 1)
}
That should work, though I have been having trouble doing that with even GetKeyDown, where it causes the key to trigger multiple times. I made something I call a keySwitch to prevent GetKeyDown from executing the same code on a single key press.

1 Like

Oh yeah, I see, thank you so much @wcoltd. The thing is, I always have problems with these:

I guess that’s because I’m not THAT familiar with Unity and how scripting communicates with the engine.
Anyway, thanks again for the help, I’ll use this ASAP :thumbsup:

If you want a non-programmatic approach then you can do you by simply adding an Audio Source from Game Object -> Audio -> Audio Source

Then Dragging the Audio from your assets folder to to Audio Clip (On the right and highlighted) Attribute in Inspector.

1 Like

Privacy & Terms