Tuesday, December 2, 2014

Unity how to bring music to another scene

Here is how i do it

1. Let say you start at Scene1,  Create an empty object, name it "empty"
2. attach a new component "audio source" to your empty object, drag a music into your "audio source", tick the "play at awake"
2. attach a script to your empty object, name it "music.js"
3. open your music.js and put in below code

 function Awake()
 {
   
    DontDestroyOnLoad(gameObject);
    
 }


Now when you start your game at scene1, your music is playing, go to the next scene, your music is still playing

how to mute the song
==============
1. let say you now in scene 3, you wanna mute the song
2. so you do it in the scene 3 script as below

var gameMusicGameObject;
var hahaaudioAudioSource;


function Start () 
{

    gameMusic = GameObject.Find("empty");
    hahaaudio = gameMusic.GetComponent(AudioSource);


}

//THE PLACE WHEN YOU CLICK BUTTON, MUTE IT


    hahaaudio.mute=true;
          


No comments:

Post a Comment