Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 sadowlight123 · Apr 06, 2018 at 10:58 AM · staticdontdestroyonload

static and Don't Destroy On load

Hello all ,

I am trying to make a static class that I could access from any other unity script in my game and I want it also to not destroy while moving from scene to scene.

The main idea is that I have a background music controller having functions such as :

 public void SetAudioSrc()
     {
         AudioClip audio = BackgroundMusic[Random.Range(0, BackgroundMusic.Length)];
         audiosrc.PlayOneShot(audio);
     }
 
     public void SetMax()
     {
         audiosrc.volume = Max;
     }
 
     public void SetVolume(float Volume)
     {
         audiosrc.volume = Volume * Max;
     }
     

I want to be able to access these functions from any other script using :

 BackgroudMusicController.Instance.SetVolume(0.4);

For example in my UI script I have (this will be called upon pressing save button after editing a slider for music volume):

 public void SaveOptions()
     {
         Slider VolumeSlider = GameObject.Find("VolumeSlider").GetComponent<Slider>();
         if (VolumeSlider != null)
         {
             BackgroundMusicController.Instance.SetVolume(VolumeSlider.value);
         }
 
     }

However, destroying the BackgroundMusicController on each level will reset the music which is something I don't want so that is why i added don't destroy on load . The full script is as follows now

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BackgroundMusicController : MonoBehaviour {
 
     private static BackgroundMusicController _instance;
 
     public static BackgroundMusicController Instance
     {
         get
         {
             if (_instance == null)
             {
                 _instance = GameObject.FindObjectOfType<BackgroundMusicController>();
                 DontDestroyOnLoad(_instance);
             }
 
             return _instance;
         }
 
     }
 
 
     public AudioClip[] BackgroundMusic;
     public AudioSource audiosrc;
     public float Max;
 
     // Use this for initialization
     void Awake()
     {
         audiosrc = GetComponent<AudioSource>();
         SetAudioSrc();
         SetMax();
 
 
        
        
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
 
     public void StartitUp()
     {
         return;
     }
     public void SetAudioSrc()
     {
         AudioClip audio = BackgroundMusic[Random.Range(0, BackgroundMusic.Length)];
         audiosrc.PlayOneShot(audio);
     }
 
     public void SetMax()
     {
         audiosrc.volume = Max;
     }
 
     public void SetVolume(float Volume)
     {
         audiosrc.volume = Volume * Max;
     }
     
 
 }


I place the BackgroundMusic script on an empty gameobject on the MainMenu Scene

But the main issue is that now when I am going between scenes as follows :

  • MainMenu (working)

  • OptionMenu (working)

  • MainMenu (i have two of the gameobject : the one that is placed
    by default in the scene and the one
    that was kept with don't destroy on
    load)

this is problem 1

and problem 2 is that I am obliged to call any function at least once in the BackgroundMuiscController from another script so that the dontdestroyonload works , otherwise it doesn't work at all.

Please help Have a good day :D

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by davidcox70 · Apr 06, 2018 at 11:37 AM

There are a couple of approaches to avoiding the re-creation of multiple DontDestroyOnLoad objects. You could create a starting scene which is only passed through once (such as a start-up splash screen) where your dontDestroy objects are created. Or you could set your gameObject as a pre-fab, loading it into your scene only if it isn't there already (from a previous instantiation). Or perhaps you could do it at script level to avoid the double-creation, in which case I think you would probably need to check for a previous instance in Awake(), and not re-create if another instance is found (not tested):

 public class BackgroundMusicController : MonoBehaviour {
     
     public static BackgroundMusicController Instance;
     
     void Awake(){
         if (Instance!=null){
             GameObject.Destroy(Instance);
     } else {
         Instance = this;         
         DontDestroyOnLoad(this);
     }
 }
 
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 sadowlight123 · Apr 06, 2018 at 11:45 AM 0
Share

Thank you this worked

avatar image
0

Answer by Timoty75 · Feb 04, 2019 at 12:26 PM

Thank a lot but for put the object in a certain point of another scene? thanks again!

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 JTAGames · Mar 26, 2021 at 12:48 PM 0
Share

We actaully have a tutorial on how to put your character in the right position between scenes and you can back and forth between as many scenes as you want with the one character.

https://www.youtube.com/watch?v=U18j9t0XkaA

We also have a tutorial on how to make one don't destroy on load script that you can put on as many game objects as you want.

https://www.youtube.com/watch?v=HXaFLm3gQws

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.

Follow this Question

Answers Answers and Comments

80 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 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 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 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

Static problems 0 Answers

Working between different scenes! 1 Answer

Static calls to Static GameObject 2 Answers

c# Static class dont destroy on load not working 1 Answer

How does one inspect static vars? 3 Answers


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