AudioSource destroyed on load

So I’m just in the process of cleaning up my blockbreaker before sharing it with the world and wanted to add some additional sfx to make the game seem a bit more professional. So there are 3 sounds I want to implement that are causing me trouble as they are destroyed before they can finish playing.

  1. A sound when the ‘Start’ button is pressed,
  2. A sound when the ball hits the lose collider, and
  3. A sound when the player clears the level.

Each of these occur just before a new scene is loaded, thus destroying them and cutting the sound short.

I have tried to use the DontDestroyOnLoad() command in various iterations, but cannot get it to work. Does anyone know the correct way of doing this?

~Thom

Hello @MooseLucky, how are you?

I’m unable to do some experimentation with this right now since I’m at work, if we don’t get it solved until tonight I’ll create a project to try it out.

Which method are you using to play these sounds? would you mind sending a screenshot of the inspector of a GameObject with at least one of those sounds and another one of the script calling it?

Is it the case where the GameObject that has the AudioSource component attached to it is destroyed (which happens on a level change, for example)? Then the audiosource goes with it. The solution I use for that situation is

AudioSource.PlayClipAtPoint()

which doesn’t depend on having an audiosource attached to anything in the level. The “point” where I play it is Camera.main.transform.position so it acts like a 2D audiosource, playing right at the camera’s audiolistener.

@Todd_Vance

Yes, I have been using PlayClipAtPoint, but this doesn’t work, as when a new scene is loaded all objects are destroyed including the audio.

@Joao_Dalvi

I will give me info tomorrow when I have some time!

Moose,

I would guess that the problem is with the PlayClipAtPoint() method, the problem is that PlayClipAtPoint instantiates a GameObject in that specific position to play the clip, and this is a regular object that will get destroyed on load.

Perhaps you should create another GameObject that is persistent through all the game (or use one that is already persistent), and add the clips to this GameObject, and when you press the button you can play the sound inside this persistent gameObject using other methods such as AudioSource.PlayOneShot.

Yes! This is the idea I had yesterday. To create a SoundManager object that could play the effects without being destroyed.

I will feedback with my results!

1 Like

Hey Moose,

So, did it worked?

Hey! Thanks for your interest!

I was meant to get back sooner, but it has been a bit hectic over Christmas!

It worked! I will post the script below!

I managed to solve the problem by creating a game object that is responsible for sounds.

using UnityEngine;
using System.Collections;

public class SoundFX : MonoBehaviour {

static SoundFX instance = null;

//Each soundeffect can be defined in the inspector
public AudioClip start;
public AudioClip death;
public AudioClip win;

//Functions exactly the same as the MusicManager
void Awake () {
	AudioSource audio = GetComponent<AudioSource> ();
	if (instance != null) {
	Destroy(gameObject);
	print ("SFX destroyed!");
	}
	else {
	instance = this;
	GameObject.DontDestroyOnLoad(gameObject);
	}
}

//We can simply call these functions using messages from relevent scripts
public void PlayStart(){audio.clip = start; audio.Play ();}
public void PlayDeath(){audio.clip = death; audio.Play();}
public void PlayWin(){audio.clip = win; audio.Play();}

}

Thanks everyone for your help - it was quite satisfying to fix this!

Happy New Year. :stuck_out_tongue_winking_eye:

1 Like

I`m glad that you made it work :slight_smile: it is indeed very satisfying!

Privacy & Terms