- Home /
Too subjective and argumentative
Play Sound On Object In Another Scene With Button from First Scene
OK This is going to sound a bit confusing so please bear with my while I try and explain. ;) OK lets say I have a scene we'll call it scene one for simplicity. In this scene I have a UI panel with buttons that I want to play a sound clip with.
Now here comes the possible confusing part.
However I want to play the sounds that my buttons select in scene one on a game object located in scene two. So that when you select a sound in scene one, scene two will load and play that sound on my game object. So far in scene 2 I have this script that when a sound is done playing the scene one will automatically load back up so that another sound can be clicked and then played.
I hope I explained that right.
This is the script I have in scene two, scene one is currently just buttons with sound selections.
using UnityEngine;
using System.Collections;
public class WaveformAudioManager : MonoBehaviour {
//Exit Waveform Screen after audio has finished playing to Main Logo screen
void Start()
{
audio.Play();
StartCoroutine(LoadAfterDelay1("KnightSpinningLogo_NoKITTintro"));
}
IEnumerator LoadAfterDelay1(string levelName){
//yield return new WaitForSeconds(2.5f); // wait 1 seconds
yield return new WaitForSeconds(audio.clip.length); //Waits till Audio is done playing
Application.LoadLevel(levelName);
}
}
Answer by TRG96 · Feb 26, 2015 at 05:20 PM
you can use this function to keep the script and load it into the next scene
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
In the script have all the different sounds and check which sound the player clicked on, keep the script and object and take it to the next scene and play the selected sound.
Be careful using DontDestroyOnLoad because if you put it on your script in scene 1 you will have a duplicate every time you are going to come back from scene 2 to scene 1.
I would suggest you to make another scene (let's call it scene 0) with a single gameobject inside that will be used to carry your information from scene 1 to scene 2 and back . You can put a simple script on this gameobject in scene 0 that contains the sound you want to play. Also add this DontDestroyOnLoad line to keep it alive between scene 1 and scene 2.
DontDestroyOnLoad(this);
AudioClip soundToPlay;
Then when you select your sound in scene 1 store it in this script. When you load scene 2 just play it from this script.
Ok It seems By using the don't destroy on load I can get a sound to play in the next scene only in my game object I have a line renderer that has a Audio Visualizer script attached, now in my 2nd scene the audio play but my line renderer does not react to the sound??? I get this error message:
NullReferenceException: Object reference not set to an instance of an object WaveformAudio$$anonymous$$anager+c__Iterator61.$$anonymous$$oveNext () (at Assets/WaveformAudio$$anonymous$$anager.cs:37) UnityEngine.$$anonymous$$onoBehaviour:StartCoroutine(IEnumerator) WaveformAudio$$anonymous$$anager:Start() (at Assets/WaveformAudio$$anonymous$$anager.cs:30)