- Home /
UI Button deletes Function on start
I'm trying to play a sound when I press a button. It plays the first time, but when I go back to the same scene and press it again it doesn't play. I think that I've found the problem, but I have no idea on how to solve it. Here is the script for the music:
using UnityEngine;
using System.Collections;
public class ButtonSound : MonoBehaviour {
public AudioClip buttonSound;
private static ButtonSound instance = null;
private AudioSource source;
public static ButtonSound Instance{
get { return instance; }
}
void Awake() {
source = GetComponent<AudioSource>();
if(instance != null && instance != this) {
Destroy(this.gameObject);
return;
}else{
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
public void Play(){
source.Play();
}
}
On the button (UI) I'm using pointer down to start the "Play" function.
From what I can find out the problem is that when I start the scene the script is there, but when I go back to the same scene it's gone. (I've included pictures of the before and after):
Do you have a UI element and a prefab and are they different?
It still makes little sense, if you come back into a scene it should load the same initial UI element.
Also I have no idea what you're trying to do in the Awake function, can you give a bit more info?
The only thing I can think is it's calling the Destroy command but no idea why you need the Destroy command.
EDIT
Tip, edits are a good idea to limit what might be a long list of comments, just a tip nothing to do with the question.
It can't play it if it's not there, that After image shows the component is missing, I still don't understand why or why you're using this:
if(instance != null && instance != this) {
Destroy(this.gameObject);
return;
}else{
instance = this;
}
EDIT
O$$anonymous$$ I think I need to understand more about this, just how many game objects are you creating? Do you really need to destroy them? Can't you just delay the load new scene until after the sound plays?
Oh yeah, sorry. The reason that I'm using that is to destroy the extra instances of the gameObject, I'm not sure that it's working like it's supposed to though...
EDIT
I've tried delaying loading the new scene until the sound is done playing, but it doesn't work.
Here is the script for the sound GameObject: public AudioClip buttonSound; private AudioSource source; public static bool hasPlayed = false;
void Awake() {
source = GetComponent<AudioSource>();
}
void Start(){
hasPlayed = false;
}
public void Play(){
source.Play();
if(source.isPlaying == false){
hasPlayed = true;
}
}
And here is the script that's loading the level:
public void loadLevel(){
if(ButtonSound.hasPlayed)
Application.LoadLevel(level[Random.Range(0, level.Length - 1)]);
}
Let's try and get this as basic as we can. Are you O$$anonymous$$ referencing variables in other scripts?
If so when the sound starts playing pass a float back to the loadLevel script that's the sound start time + sound start length, so if your sound plays for 1.5f seconds:
endPlaying = Time.time + 1.5f;
pass that to the loadLevel script and load the level if Time.time > endPlaying
Answer by bpaser3 · Nov 19, 2015 at 07:05 PM
Have you tried putting the code under Void OnLevelWasLoaded () instead of Void Awake(), I was having a very similar problem and that worked for me.