- Home /
GUI Elements Carry Over To Next Scene
I couldn't really think of a good way to title this problem as it's an interesting one.... maybe someone has a simple solution for something like this. OK here goes.
I have a music player in once scene that has a lot of GUI elements like a volume slider and a long bar that shows the songs progress. The script also appears to talk to other scripts for things like the buttons which have their own script.
Now here is where my problem gets interesting, I was trying to figure out how to make the music continue to play when I click a button and change my scene.
I tried the don't destroy on load idea which did work as far as allowing the music to continue to play across scene changes. However it also carried over to the next scene the volume control slider and song progress bar. Which I don't want.
On another note and I should really leave this alone for another question but the two are related.
In my music player I have a spectrum data GUI text that reads the spectrum data for the song that is currently being played. Now when I go back into my music player scene this has stopped working and does not work even if I click another song to play.
My whole project by the way is basically a series of scenes that are mostly just GUI stuff, textures PNG background interfaces, buttons, text the occasional spinning 3D simple model for a logo and sound FX / Music. Not sure if that helps any but what the heck. ;)
Answer by taxvi · Dec 08, 2014 at 02:59 PM
i'd make a separate object with only an audio source attached to it. then would call the DontDestroyOnLoad on the music player. also, in all the objects that have reference to the music player object i'd stick in GameObject.FindObjectWithTag() in the Start() function so that when you switch back and forth between the scenes it would restore the reference to the music player.
Thanks taxvi, It's a weird kind of set up. I'll try and give some more information on how this crazy thing is set up. I have this script on my main camera:
var music : AudioClip[];
private var isPl : boolean = false;
private var paused : boolean = false;
private var key : int = 0;
private var view$$anonymous$$ey : int = 0;
var mselect : Transform;
var scrollDelay : float = 0.5;
private var last : float;
var pm : int = 0;
function Start () {
audio.clip = music[0];
for (var i = 0; i< music.length; i++) {
var new$$anonymous$$usic = Instantiate(mselect, Vector3(1000,0,1030),Quaternion.identity);
new$$anonymous$$usic.GetComponent("ST_select$$anonymous$$usic").music = music[i];
new$$anonymous$$usic.GetComponent("ST_select$$anonymous$$usic").key = i;
new$$anonymous$$usic.GetComponent("Text$$anonymous$$esh").text = music[i].name;
new$$anonymous$$usic.gameObject.AddComponent("BoxCollider");
}
}
function FixedUpdate () {
if (Input.GetButtonDown("Vertical") != 0 && Time.time >= scrollDelay+last) {
view$$anonymous$$ey -= Input.GetAxisRaw ("Vertical");
last = Time.time;
}
view$$anonymous$$ey = $$anonymous$$athf.Clamp(view$$anonymous$$ey,0,music.length-1);
if (isPl && !audio.isPlaying && !paused) {
key++;
if (key >= music.length) {
key=0;
if (pm == 0) {
isPl = false;
} else if (pm==2) {
audio.clip = music[key];
audio.Play();
}
} else {
audio.clip = music[key];
audio.Play();
}
}
}
This script calls the select music. I'm not sure how many of these I should include that the main script accesses?
var music : AudioClip;
private var key : int;
function On$$anonymous$$ouseDown() {
if (Camera.main.audio.clip != music || !Camera.main.GetComponent("ST_main").isPl) {
Camera.main.audio.clip = music;
Camera.main.audio.Play();
Camera.main.GetComponent("ST_main").key = key;
Camera.main.GetComponent("ST_main").isPl = true;
}
}
function Update() {
transform.position.y = -key + (Camera.main.GetComponent("ST_main").view$$anonymous$$ey);
}
This script is for the Volume slider:
function OnGUI () {
GUI.Label (Rect (45, Screen.height-200, 1000, 30),"Volume");
Camera.main.audio.volume = GUI.VerticalSlider (Rect (47, Screen.height-170, 100, 100), Camera.main.audio.volume, 1.0, 0.0);
}
I THIN$$anonymous$$ this script has the music play length slider where you can drag it to any point in the music play.
private var num : int;
var pm : String[];
function OnGUI () {
GUI.Label (Rect (70, Screen.height-65, 1000, 30), "("+GetSecondsAsTime(audio.time) + " / " + GetSecondsAsTime(audio.clip.length)+") Play mode ("+pm[num]+")");
var newTime : float = GUI.HorizontalScrollbar (Rect (50, Screen.height-40, Screen.width-80, 30), audio.time, 1.0, 0.0, audio.clip.length);
if (GUI.changed) {
audio.time = newTime;
}
}
function GetSecondsAsTime(seconds) {
var sec = $$anonymous$$athf.Floor(seconds % 60);
var $$anonymous$$ = $$anonymous$$athf.Floor(seconds / 60);
if(sec < 10)
return $$anonymous$$ + ":0" + sec;
return $$anonymous$$ + ":" + sec;
}
function Update() {
num = Camera.main.GetComponent("ST_main").pm;
}
Let me know if you need to see the button scrips as well and I'll include those in another comment.
Your answer
Follow this Question
Related Questions
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
Canvas will only appear on the game screen when its child of a new loaded scene canvas 1 Answer
How to keep the same gameobject with different transform values in different scenes 1 Answer
Don't destroy On Load make object not interactable in new scene 0 Answers
Multiple EvenetSystems in Scene - only have 1 after searching though 1 Answer