Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by slader155 · Feb 09, 2015 at 12:20 PM · uibuttonscenesoundlevel

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):

before.png (26.1 kB)
after.png (24.8 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mmmpies · Feb 09, 2015 at 09:37 PM 0
Share

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?

avatar image slader155 · Feb 09, 2015 at 09:53 PM 0
Share

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)]);
     }

avatar image Mmmpies · Feb 09, 2015 at 10:51 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I check if a UI button is pressed within an if statement? 1 Answer

Button OnClick() Scripts lose assignments on scene reload 1 Answer

Playing music while scenes change? 4 Answers

Separate setting music volume and sfx in one scene 0 Answers

How To Put 2 Panel in 1 scene? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges