Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Using Singleton Class to mute soundEffects in other scene

Discussion in '2D' started by gladiuscloud, Feb 25, 2015.

  1. gladiuscloud

    gladiuscloud

    Joined:
    Jan 11, 2015
    Posts:
    44
    Now i've tested and learned to use the Singleton a bit, I've found this topic somewhere in the forum.
    Now, the question is, I'm attemping to set the audio to mute in scene 2 and my singleton class is located in scene 1, my soundEffects audioSource are attached to the gameObjects in scene2, is it good idea to use GameObject.Find("soundEffectsInScene2") //audio source to mute in my singleton class? or do u have better suggestion?... thank u

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyUnitySingleton : MonoBehaviour {
    5.    
    6. private static MyUnitySingleton instance = null;
    7. public static MyUnitySingleton Instance {
    8.     get {
    9.             return instance;
    10.         }
    11. }
    12.  
    13. void Awake() {
    14.  
    15.     if (instance != null && instance != this) {
    16.         Destroy(this.gameObject);
    17.         return;
    18.     } else {
    19.         instance = this;
    20.     }
    21.     DontDestroyOnLoad(this.gameObject);
    22.     }
    23. //---------------------------------
    24.  
    25.     void Start(){
    26.  
    27.     }
    28.  
    29.     void Update(){
    30.  
    31.  
    32.         if(Input.GetKey(KeyCode.RightArrow))
    33.         {
    34.  
    35.             AudioListener.volume = 0;
    36.    
    37.             Debug.Log("stop sound");
    38.  
    39.             }
    40.  
    41.         if(Input.GetKey(KeyCode.LeftArrow))
    42.         {
    43.    
    44.             Debug.Log("play sound");
    45.             AudioListener.volume = 1f;
    46.  
    47.         }
    48.  
    49.         if(Input.GetKey(KeyCode.DownArrow))
    50.         {
    51.             Application.LoadLevel(1);
    52.            
    53.         }
    54.     }
    55.  
    56. }
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    gladiuscloud likes this.
  3. gladiuscloud

    gladiuscloud

    Joined:
    Jan 11, 2015
    Posts:
    44
    wow, ill try sir TY:D
     
  4. gladiuscloud

    gladiuscloud

    Joined:
    Jan 11, 2015
    Posts:
    44
    I'll attach this code in my soundEffectObj with audioSource in scene2? Is that correct?
     
  5. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    You could, you could also do this in you singleton class, that is the class that knows wether the sound should be turned off..

    if you do it in your soundEffectObj in scene2 then you could also just do it in Start
    Code (csharp):
    1. void Start() {
    2.   if (MyUnitySingleton.Instance.ShouldAudioBeTurnedOff) {
    3.     audioListener.volume = 0;
    4. }
    then the code in your singleton would change to
    Code (csharp):
    1. if (Input.GetKey(KeyCode.RightArrow)) {
    2.   ShouldAudioBeTurnedOff = true;
    3. }
     
    gladiuscloud likes this.
  6. gladiuscloud

    gladiuscloud

    Joined:
    Jan 11, 2015
    Posts:
    44
    Thanks but i got this error in scene2:
    'SoundController.ShouldAudioBeTurnedOff'' is inaccessible due to its protection level