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 mattskate96 · Apr 06, 2012 at 05:57 PM · audioyieldloadlevel

Audio after button press before LoadLevel.

Hey everyone, so I've seen all the other posts about this subject, but it isn't working for me. This is in the OnGUI function :

 if(GUI.Button(Rect(90, 180, 260, 60), "Start!"))

 {
       LoadLevel();
 } 

Then in LoadLevel() function, i have this :

function LoadLevel()

{ audio.Play(); yield WaitForSeconds(1.0); Application.LoadLevel(2);

}

The problem is, the function is called on Awake and it loads Level(2) directly after a second when I start up the scene. How do I fix this?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kleptomaniac · Apr 07, 2012 at 08:20 AM

Simple answer: call your function something other than LoadLevel(). :)

Hope that helps, Klep

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
avatar image
0

Answer by Dee Va · Apr 07, 2012 at 08:32 AM

 function OnGUI ()
 {
     if(GUI.Button(Rect(90, 180, 260, 60), "Start!"))
     {
         audio.Play();           
         if (!audio.isPlaying)   
         {
         Application.LoadLevel ("1");
         }
     }

hope this helps........:)

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
avatar image
0

Answer by gregzo · Apr 09, 2012 at 09:41 PM

OnGUI cannot be a coroutine.

 function OnGUI()
 {
     if(GUI.Button(Rect(90, 180, 260, 60), "Start!"))
     
     {
           PlayMusicThenLoadLevel();
     } 
 }
 
 function PlayMusicThenLoadLevel()
 {
      audio.Play()
      yield;       //not sure this is needed
      while(audio.isPlaying) //if you want to wait until the end of the audioCLip before you load. Else, skip this.
      {
            yield;
      }
      Application.LoadLevel(yourLevelNb);
 
 }
Comment
Add comment · Show 4 · 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
avatar image mattskate96 · Apr 09, 2012 at 11:54 PM 0
Share

OnGUI wasnt a coroutine in the first place, and I tried your code, but Play$$anonymous$$usicThenLoadLevel() is still called when you start the scene. Any other ideas?

avatar image gregzo · Apr 11, 2012 at 10:38 PM 0
Share

I'm a bit puzzled... Why would Play$$anonymous$$usicThenLoadLevel() be called if you don't press the button? Function's don't just randomly get called in Awake()... And about my "OnGUI cannot be a coroutine" comment, my bad.

avatar image mattskate96 · Apr 12, 2012 at 12:32 AM 0
Share

i know right, its weird, i tried a bunch of different arrangements, like adding a bool thats starts off false and goes true when you press the button, but that didnt seem to work too great...

avatar image gregzo · Apr 12, 2012 at 08:11 AM 0
Share

You must be calling the function from somewhere. As I said, functions don't just randomly get called for no reason... Did you try a debug in the function? $$anonymous$$ake sure it is really what is happening? Check all your scripts for a forgotten call somewhere?

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Loading level after sound plays 1 Answer

coroutine or yeild for OnGUI? 0 Answers

How do I play a random sound each 5 seconds? 1 Answer

How to Make the Game Go to the Next Level When the Music Stops 2 Answers

Issues with WWW resource loading 1 Answer


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