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 kk93 · May 30, 2014 at 03:37 PM · audiodontdestroyonload

Making a reference to a script that DontDestroyOnLoad

Hello,

I am trying to make a script called "AudioFactory" that handles all audio related operations for my game (music & SFX). To ensure that it exists throughout the game, I placed the script in an empty game object called "_persistents," and called DontDestroyOnLoad(gameObject) in its start method. The problem I am now having is making a reference to it so that scene specific scripts can call its methods, because _persistent is instantiated in the MainMenuScene.

I have also tried making the methods static, but this requires (I think) that all variables are static, which is not what I want.

Is there a way to make a reference to a script that DontDestroyOnLoad? And also, am I approaching audio handling correctly, or is there a better way to approach it?

Thanks!

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
3
Best Answer

Answer by supernat · May 30, 2014 at 04:34 PM

I was going to suggest making it mostly static, but you already thought of that. So I'll just suggest making the instance static (make it a singleton). Something like this:

 private static AudioFactory _instance;

 void Awake() {
     _instance = this;
 }

 public static AudioFactory GetInstance() { return _instance; }

Now you can access the script that is attached to a single game object by calling AudioFactory.GetInstance(). Just make sure you don't attach it more than once. In fact you can add this to help with that:

 void Awake() {
     if (_instance != null) {
         Debug.Log("Dummy, you attached me twice!");  // Sorry that's how I talk to myself through code!
         return;
     }
     _instance = this;
 }
Comment
Add comment · Show 3 · 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 kk93 · May 30, 2014 at 04:45 PM 0
Share

Works like a charm. I remember you helping me back when I was having difficulty setting up my android device - thanks again!

avatar image supernat · May 30, 2014 at 05:06 PM 0
Share

No problem, glad to help. If you have some spare change, you might also check out the Asset Store. There are quite a few good audio plugins on there, some that will randomly choose music/sound effects, handle object pooling, etc.

avatar image MMGDev · Nov 15, 2020 at 01:48 AM 0
Share

thanks man! you saved my day(night) i was about to give up

avatar image
0

Answer by SkillcraftHD · Dec 30, 2020 at 10:24 PM

Hi somehow it still gives me a NullReference Exception :(

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

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

23 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

Related Questions

Menu music stops playing after returning from game level 0 Answers

How to STOP music when loading a SPECIFIC SCENE? 3 Answers

Don't destroy a variable when changing scene 2 Answers

How to stop audio in a scene when DontDestroyOnLoad was already called? 1 Answer

MissingReferenceException in AudioSource on DontDestroyOnLoad GameObject 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