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 OctoSloths · Dec 09, 2014 at 01:53 AM · c#sceneaudiosourcedontdestroyonload

How to check scene number and use it in an If statement?

I have an audio that is set to DontDestroyOnLoad() but I want it to destroy on certain scenes, is it possible to check that and make the audio only DontDestroyOnLoad() if it is a certain scene?

Here is my script currently:

 using UnityEngine;
 using System.Collections;
 
 public class dialogueclassicexist : MonoBehaviour {
     void Awake() {
         DontDestroyOnLoad(gameObject);
     }
 }
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
4
Best Answer

Answer by msnyder102 · Dec 09, 2014 at 05:00 AM

Awake is only called once, when the behavior is loaded, therefore it is not checking every scene because you are not loading a new one every scene. Try something like this.

  using System.Collections;
  public class AudioGSingle : MonoBehaviour {
      private static AudioGSingle instance = null;
      public static AudioGSingle Instance {
          get { return instance; }
      }
      void Awake() {
          if (instance != null && instance != this) {
              Destroy(this.gameObject);
              return;
          } else {
              instance = this;
          }
          DontDestroyOnLoad(this.gameObject);
      }
      void OnLevelWasLoaded(int lvlNum){
          if(Application.loadedLevelName == "gameover"){
             Destroy(this.gameObject);
          }
      }
 }
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 OctoSloths · Dec 09, 2014 at 05:54 AM 0
Share

Thank you!

avatar image DiegoMontania · Sep 10, 2015 at 12:11 AM 0
Share
 if(Application.loadedLevelName == "gameover")
 {
      Destroy(this.gameObject);
 }

Works for me, thanks!

avatar image residentrayman · Jan 16, 2016 at 04:35 PM 0
Share

Thanks alot bro

avatar image
1

Answer by theLucre · Dec 09, 2014 at 02:23 AM

Use the properties Application.loadedLevelName for String or Application.loadedLevel for int index.

http://docs.unity3d.com/ScriptReference/Application-loadedLevelName.html http://docs.unity3d.com/ScriptReference/Application-loadedLevel.html

 if( Application.loadedLevelName == "MyScene")
     DontDestroyOnLoad(gameObject);
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 OctoSloths · Dec 09, 2014 at 03:13 AM 0
Share

I tried your script and it doesn't seem to work, although I get no error messages. I currently have this script:

 using UnityEngine;
 using System.Collections;
 
 public class audiolevel1 : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start () {
         if (Application.loadedLevelName == "hiding1")
             DontDestroyOnLoad (gameObject);
     }
     }
 
avatar image
1

Answer by Cerbion_ · Dec 09, 2014 at 02:48 AM

theLucre is completely right with his answer, however you can also make use of the inbuild function:

 void OnLevelWasLoaded(int level)
 {
     if(level == 123)    // the scene number
     {
         // Do something
     }
 }

OnLevelWasLoaded() is automatically called whenever (take a guess) a level is loaded! (Suprise!)

Hope this helps~

Cerbi

Comment
Add comment · Show 2 · 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 OctoSloths · Dec 09, 2014 at 03:44 AM 0
Share

Unfortunately, neither seem to be working. :( Currently, I'm trying to make it so that it destroys only on the gameover scene. Here is my current script:

 using System.Collections;
 public class AudioGSingle : $$anonymous$$onoBehaviour {
     private static AudioGSingle instance = null;
     public static AudioGSingle Instance {
         get { return instance; }
     }
     void Awake() {
         if (instance != null && instance != this) {
             Destroy(this.gameObject);
             return;
         } else {
             instance = this;
         }
         if( Application.loadedLevelName == "Ghost"){
             DontDestroyOnLoad(this.gameObject);
         }
         if( Application.loadedLevelName == "gameover"){
             Destroy(this.gameObject);
         }
         }
 }
avatar image OctoSloths · Dec 09, 2014 at 05:18 AM 0
Share

Thank you so much!! :)

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

28 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

Related Questions

MissingReferenceException in AudioSource on DontDestroyOnLoad GameObject 0 Answers

Facebook posting problem in multiple scene architechture 0 Answers

How do I transfer a gameobject created in one scene to another scene AND use it in a c# script? 1 Answer

I am making a 2D game and I need to repeat the sound from 1 to 2 scenes. (to keep it going) sorry for my english 1 Answer

GameObject::scene and DontDestroyOnLoad() 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