Dynamic DoNotDestroyOnLoad

Okay so basically I am making a small game - what I want is the music to continue throughout the game even if you die or change scene. Furthermore, when a scene starts I want to play a bit of story intro, in the form of audio, and some captions with to make it more easy to follow the story. However, I do not want the story audio and captions to play each time you die, as that will just be annoying. To avoid that I used DoNotDestroyOnLoad, which works fine for this:

private static bool created = false;


	void Awake () {
		if (!created) {
			DontDestroyOnLoad(this.gameObject);
			created = true;
		} else {
			Destroy(this.gameObject);
		} 
	}

The music, intro audio, and captions are all gameObjects which are children of a single game object which is the persistent one.

So right now if I start the game, the music starts, the intro audio and captions start, and if I die the music continues and the audio and captions does not play again. However, when I change scene I’m faced with the problem that the captions and audio does not play, as the gameobject is so persistent that it does not care that the scene has changed. And even if it did change the intro audio and captions would be the ones from the level 1, and not level 2, as they are never assigned correctly (the gameobject does not dissappear so it is stuck with the same values) - do I have other options than using DoNotDestroyOnLoad? or do I have to make some wierd workaround checking which level I am on and if the audio and captions has been played at this level before?

Best Regards
Olle

Hi, I don’t think there is any other way of implementing your design, you’re right to use DoNotDestroyOnLoad. I would simply destroy the gameobject in question and set created to false upon level progression, allowing the next level to keep its instance of the gameobject and script. I hope that helps =)

I made this script that resets the level - instead of reloading it.

using UnityEngine;
using System.Collections;

public class ResetScript : MonoBehaviour {

	public GameObject[] levers;
	public GameObject[] elevators;
	public GameObject[] platforms;
	public GameObject player;
	public GameObject camera;

	// Use this for initialization
	void Start () {
		levers = GameObject.FindGameObjectsWithTag("Lever") as GameObject[];
		elevators = GameObject.FindGameObjectsWithTag("Elevator") as GameObject[];
		platforms = GameObject.FindGameObjectsWithTag("Platform") as GameObject[];
		player = GameObject.FindGameObjectWithTag("Player");
		camera = GameObject.FindGameObjectWithTag("MainCamera");
	}
	
	public void reset(){
		player.transform.position = Vector3.zero;

		foreach(GameObject lever in levers){
			lever currentLeverScript = lever.GetComponent<lever>();
			if(currentLeverScript.operated == true){
				currentLeverScript.operated = false;
				if(!lever.animation.isPlaying){
					lever.animation["InteractLever"].speed = -1;
					lever.animation["InteractLever"].time = lever.animation["InteractLever"].length;
					lever.animation.Play();
				}
			}
		}
		foreach(GameObject elevator in elevators){
			elevator.transform.position = elevator.GetComponent<ElevatorScript>().startPosition;
		}
		foreach(GameObject platform in platforms){
			platform.transform.position = platform.GetComponent<PlatformScript>().startPosition;
		}
	}
}

This is considered solved