• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ollenator · May 26, 2014 at 11:22 AM · static

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Chris_Dlala · May 26, 2014 at 01:15 PM

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 =)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Ollenator · May 26, 2014 at 02:18 PM

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;
         }
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Ollenator · May 26, 2014 at 02:18 PM

This is considered solved

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sprawl · May 26, 2014 at 02:20 PM 0
Share

You should confirm a solution then.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to make easily accessible classes. the type like Unity has in JS for example. 1 Answer

Static IEnumerator!? 1 Answer

C# Going Static or Going OOP 3 Answers

trying to delay a loop using yield - not working 1 Answer

How do we access static prefabs? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges