Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 zahidashahid535 · Nov 10, 2021 at 07:25 AM · dontdestroyonload

Gameobject DontDestroyonLoad references not working after changing scene

Hi, need help in referencing objects in the inspector.

I have 2 scenes I,e main_menu and level. At scene main_menu there are a couple of game objects that are dontdestroyonload. on those objects sound effects and game music and scripts are attached. When I select the level from the main menu it works fine till now I can play game without any error or issue which is scene 2 l(named level 3). when I go back to scene 1 that is the main menu references in the inspector to the objects under objects remove or null it says missing object but object is present under dontdestroyonload . As in image 2.

alt text

alt text

1.png (168.9 kB)
2.png (163.8 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rh_galaxy · Nov 10, 2021 at 11:23 AM

I guess that this is why... It was like this for me at least, before I fixed it.


When you go back to another scene working the first time, the do-not-destroy object may/will be created again. And if you don't want that you must also make the objects Awake() function to "fix" this by destroy the new object (not the old one from before, or you will invalidate the references you hold).

This is how it can be done

 public static GameManager theGM = null;
 //first code to run
 void Awake()
 {
     //singleton
     if (theGM == null)
     {
         theGM = this;
     }
     else if (theGM != this)
     {
         //enforce singleton pattern, meaning there can only ever be one instance of a GameManager.
         Destroy(gameObject); //<- Be careful, this makes OnDestroy() be called and we don't
         // want to deinit everything there (for example if you have external things setup the
         // first time that you want to keep function)
         return;
     }
     
     //the rest is done once only...
     DontDestroyOnLoad(gameObject);
     
     //...
 }
 
 //example:
 //cannot do this, because it is called when Awake is called a 2nd time when loading another scene!
 // so OnDestroy() gets called for the new object that is then destroyed to enforce singleton
 //we only want to do this when the app exits
 /*private void OnDestroy()
 {
     if (!bSteamAPIInited)
         return;
     SteamAPI.Shutdown();
 }*/


If this doesn't help you, share the relevant code, so we can look at it.

Comment
Add comment · Show 5 · 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 zahidashahid535 · Nov 11, 2021 at 04:48 AM 0
Share

In Awake funtion checking for instance null or not still not working

public class MusicSetting : MonoBehaviour { public AudioSource audioSrc; public AudioMixer audioMixer; public Toggle MusicToggle; static MusicSetting instance = null; private float musicVolume = 1f;

 private void Awake()
 {
     
     if (instance != null)
     {
         Destroy(gameObject);
     }
     else
     {
         instance = this;
        
     }
     DontDestroyOnLoad(gameObject);
 }
 void Start()
 {
     // Assign Audio Source component to control it
     audioSrc = GetComponent<AudioSource>();
     CheckMuteOrUnMute();
 }
  
 void CheckMuteOrUnMute()
 {
     if (PlayerPrefs.GetInt("MusicMute", 0) == 0)
     {
         Debug.Log("Music Un Mute");
         audioSrc.mute = false;
         MusicToggle.isOn = false;
     }
     else
     {
         Debug.Log("Music Mute");
         audioSrc.mute = true;
         MusicToggle.isOn = true;
     }
 }
 public void MuteMusic(bool muteMusic)// Update player prefrences
 {
     if (PlayerPrefs.GetInt("MasterMute", 0) == 0)
     {
         Debug.Log("Master music is unmute");
         audioSrc.mute = muteMusic;
         if (muteMusic)
         {
             PlayerPrefs.SetInt("MusicMute", 1); 
         }
         else
         {
             PlayerPrefs.SetInt("MusicMute", 0); // Music is unmute
         }
     }
    
     else
     {
         Debug.Log("Master music is mute");
         Debug.Log("so music is mute");
         PlayerPrefs.SetInt("MusicMute", 1); 
     }
 }
 public void VolumeofMusic(float volume)
 {
     audioSrc.volume = volume;
     Debug.Log("audioS.name " + audioSrc.name);
     Debug.Log("audioS.volume " + audioSrc.volume);
     Debug.Log("volume " + volume);
 }
 

}

avatar image rh_galaxy zahidashahid535 · Nov 11, 2021 at 05:08 AM 0
Share

You are missing the return in your instance check... Edit: try also to make it exactly as my first suggestion, I think there is a difference

 void Awake()
 {
     if (instance == null)
     {
         instance = this;
     }
     else if (instance != this)
     {
         Destroy(gameObject);
         return;
     }
     DontDestroyOnLoad(gameObject);
 }
avatar image zahidashahid535 rh_galaxy · Nov 11, 2021 at 06:51 AM 0
Share

i tried but still the same problem. Other object references working fine but the issue is with the objects under DontDestroryOnLoad.alt text

Show more comments

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

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

donotdestroy for hud - but hud appears on main menu :( 1 Answer

how to Undo DontDestroyOnLoad 1 Answer

Will DontdestroyonLoad() Keep Child Objects too 2 Answers

Keeping score problems dontdestroyonload 2 Answers

GUI Elements Carry Over To Next Scene 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