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 bvance800 · Jul 20, 2020 at 10:03 AM · callbackmissingreferenceexception

Reference to script in callback goes missing when scene reloads

TL;DR I have a GameManager Object with an AudioManager script. The object is set to not be destroyed on reload. Though a reference to it goes missing from a menu when the main menu scene reloads.

This may be a misunderstanding on my part of how DontDestroyOnLoad(gameObject); works, but i have a settings menu with a button that controls the music playing in the background.

alt text After one round of game play (gameplay being in another scene) and returning to the main menu scene, the object holding the audiomanager script that is supposed to be referenced in the music toggle button is missing.

alt text But, I have that gameobject set to not be destroyed on load. What could the issue be? Or what is my misunderstanding here?

Edit: I realized i was basically making my own OnClick event, so i tried just adding one for the ToggleMusic function and the same result occurs, the GameManager object itself becomes missing. I tried turning the GameManager into a prefab but nothing changed. So the bug persists.

untiy-error2.png (111.9 kB)
untiy-error.png (127.8 kB)
Comment
Add comment · Show 1
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 bvance800 · Jul 19, 2020 at 05:47 AM 0
Share

FIXED: TL;DR: make the callback object a prefab. $$anonymous$$ake sure you are referencing the correct thing to avoid null reference exceptions.

So here's what i did. I found others saying that turning the gamemanager object into a prefab and then dragging the prefab (not from the scene hierarchy) into the callback would work. However i kept getting null reference exceptions whenever i tried to Play or Stop sounds from the audiomanager script on the gamemanager prefab. (Game$$anonymous$$anager prefab contains Audio$$anonymous$$anger script, which is a singleton)

 public class Audio$$anonymous$$anager : $$anonymous$$onoBehaviour
 {
     public Sound[] sounds;
 
     public static Audio$$anonymous$$anager instance;
 
     // Start is called before the first frame update
     void Awake()
     {
         if(instance == null) {
             instance = this;
         }
         else {
             Destroy(gameObject);
             return;
         }
 
         DontDestroyOnLoad(gameObject);
 
 
         foreach(Sound s in sounds) {
             s.source = gameObject.AddComponent<AudioSource>();
             s.source.clip = s.clip;
             s.source.volume = s.volume;
             s.source.pitch = s.pitch;
             s.source.loop = s.loop;
         }
 
         
     }

Problem has to do with how i was referencing the list of sounds that is on the Audio$$anonymous$$anager. I was previously using the line:

 Sound s = Array.Find(sounds, sound => sound.name == name);

But i changed it to:

 Sound s = Array.Find(Audio$$anonymous$$anager.instance.sounds, sound => sound.name == name);

I think because i was using the prefab and not the Game$$anonymous$$anager instance (even though it is made from the prefab) it wasn't finding the sounds array that i had on the prefab script so it needed to reference the instance on the gameobject made from the prefab. Im not even sure if thats it but i know that the issue is fixed!

0 Replies

· Add your reply
  • Sort: 

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

130 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

Related Questions

Looking for a callback when unity saves 1 Answer

OnDestroy() callback in Editor upon deleting selected GameObject - del key 5 Answers

WWW database callback 1 Answer

AssetModificationProcessor OnWillDeleteAsset not called 1 Answer

Where should I override the Network Manager Callbacks? 0 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