0
votes

In my game, I have two scene. What I want to achieve is if user navigates from one scene to another, background audio specific to each should be played from start(audio length=0)

But all my efforts are in vain.

I tried using 'Pause' Method of audioSound I tried create a new game object and assign this scene background score to it and play destroy gameObject created for another scene if there was any

But it doesn't give the result that I want.

I searched for finding how to play audioClip from start and stop other audioClip playing but didn't find any.

I know I'm not supposed to ask for code on stack overflow but if anyone has achieved this or has some pseudo code request you to provide it

2
How about just have GameObjects in each scene which have an Audio Source with the appropriate clip attached to them, which just Play On Awake? That's the easiest non-scripting solution here - if you need code, you need to show us how you've already approached this, and point out the parts that don't work as you intended them. Otherwise, we have no context and little idea of how you have already attempted a solution. - Serlite

2 Answers

0
votes

I'm not sure I understand your question properly, since it seems the simplest scenario for background music.

If you really want a change of audioclip in every scene, let's say Scene A must play Clip A and Scene B must play Clip B, and both clips should be played as soon as a scene is loaded, you just need to create a game object in both scenes with an Audio Source component, with the Play On Awake flag active, and then just assign the appropriate clip for the scene (i.e.: assign Clip A in the Audio Clip field of the Audio Source component of Scene A game object, and do the same with Clip B for Scene B).

That's pretty much it.

0
votes

If you looking at the detail code then you can try this code.

First : Make a script "SoundFxScript.cs" // You can modified as you want

Insert this code :

public class SoundFxScript : MonoBehaviour {
    //Background Music

    public AudioSource Scene1_Sound;
    public AudioSource Scene2_Sound;

    // Use this for initialization
    void Start () {
        PlayBackgroundMusic ();
    }

    // Update is called once per frame
    void Update () {

    }

    public void PlayBackgroundMusic() {
        if (UnityEngine.SceneManagement.SceneManager.GetActiveScene ().name == "Scene1") {
            Scene1_SoundPlay();
        } else if (UnityEngine.SceneManagement.SceneManager.GetActiveScene ().name == "Scene2") {
            Scene2_SoundPlay();
        } 
    }

    public void Scene1_SoundPlay() {
        Scene1_Sound.Play ();
        Scene2_Sound.Stop ();
    }

    public void Scene2_SoundPlay() {
        Scene1_Sound.Stop ();
        Scene2_Sound.Play ();
    }


// Step Fifth
public void LoadTheScene (string Scenename) {
            UnityEngine.SceneManagement.SceneManager.LoadScene (Scenename);
            sf.PlayBackgroundMusic ();

        }
}

Second : Make Gameobject name = "SoundMusic" at the first scene and add component script SoundFxScript.cs. In gameobject "SoundMusic" you can add you background music for scene1 and scene2.

Third : Make a singleton file Singleton.cs

Insert this code :

public class Singleton : MonoBehaviour {
    static Singleton instance = null;

    void Start ()
    {
        if (instance != null)
        {
            Destroy(gameObject);
            return;
        }

        DontDestroyOnLoad(gameObject);
        instance = this;
    }

}

Fourth : at gameobject "SoundMusic" add component script "Singleton.cs"

Fifth : How To Call In Another Scene (Load Scene). This method is inside SoundFxScript.cs

Example You have a method to call a load scene. Try this method :

Call it with : LoadTheScene("Scene2") // Call scene2

In here you can call your SoundFxscript.cs Component from any script you have.

Example :

SoundFxScript sf;
sf = GameObject.Find ("SoundMusic").GetComponent<SoundFxScript> ();

And you can use method LoadTheScene to load a new scene and the background music will RePlay again according to the what Scene is it.

That's All.