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 Sipi2000 · Jul 09, 2014 at 01:13 PM · c#audio

What's wrong with this "menu" script?

Sorry for bad English So i wrote a script to create my games menu. The script is attached to my Main Camera(the cam is the audio source and listener too)Everything works fine exept two things. **1.**I can't hear the background music 2.If i click on "back" in the "Options" menu it creates a second cam and a third...etc. I know why is this happening(i mean the 2.) but i can't solve it.Only scene #0 has a Main Cam other scenes not(i have 3 scenes) So here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class Menu : MonoBehaviour {
     public float sx_box; //Starting x point of the background box
     public float sy_box; //Starting y point of the background box
     public float ex_box; //Ending x point of the background box 
     public float ey_box; //Ending y point of the background box
 
     public float sx1; //Starting x point of a button or slide
     public float sy1; //Starting y point of a button or slide
     public float ex1; //Ending x point of a button or slide
     public float ey1; //Ending x point of a button or slide
 
     public float sx2; //Starting x point of a button
     public float sy2; //Starting y point of a button
     public float ex2; //Ending x point of a button
     public float ey2; //Ending y point of a button
 
      int level; // Number of the current scene(0-Menu 1-Options 2-Game)
 
     public float x; // The value of the volume
 
     void OnGUI () {
         DontDestroyOnLoad(transform.gameObject); //Keep the camera because i want the music to continoue after changing scenes
         level = Application.loadedLevel;
         audio.Play();
         if (level == 0)
         {
             GUI.Box(new Rect(sx_box,sy_box,ex_box,ey_box), "ICT");
             if(GUI.Button(new Rect(sx1,sy1,ex1,ey1), "Start")) {
             Application.LoadLevel(2);
             }
             if(GUI.Button(new Rect(sx2,sy2,ex2,ey2), "Options")) {
                 Application.LoadLevel(1);
             }
             }
         if (level == 1)
         {
             x = GUI.HorizontalSlider(new Rect(625, 90, 150, 20), x, 0.0F, 10.0F);
             audio.volume = x;
             if(GUI.Button(new Rect(sx2,sy2,ex2,ey2), "Back")) {
                 Application.LoadLevel(0);
             }
         }
     }
 }



Comment
Add comment · Show 5
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 Sipi2000 · Jul 09, 2014 at 04:28 PM 1
Share

1 Thank You!

2 You are AWESO$$anonymous$$E!!! #3 What do you think of the script that's my first :P #4 Can you or somebody else help with my camera problem? Thanks

avatar image Sipi2000 · Jul 09, 2014 at 05:16 PM 0
Share

I've just sent the previous comment when i said eureka. I made a new scene(called start) i will use this scene once so it wont create other cameras when i start this scene(because i only use it once) Oh man i tought "i'll test it before sending the comment" and whats happening? Yes you are right it's not working I hear the music :P I made a new scene called start(it's number 0) it automatically loads the menu but it dont loads the other scenes like Game (number 3) or Options(number 2) Here is the new script:

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$enu : $$anonymous$$onoBehaviour {
     public float sx_box; //Starting x point of the background box
     public float sy_box; //Starting y point of the background box
     public float ex_box; //Ending x point of the background box 
     public float ey_box; //Ending y point of the background box
 
     public float sx1; //Starting x point of a button or slide
     public float sy1; //Starting y point of a button or slide
     public float ex1; //Ending x point of a button or slide
     public float ey1; //Ending x point of a button or slide
 
     public float sx2; //Starting x point of a button
     public float sy2; //Starting y point of a button
     public float ex2; //Ending x point of a button
     public float ey2; //Ending y point of a button
 
      int level; // Number of the current scene(0-$$anonymous$$enu 1-Options 2-Game)
 
     public float x; // The value of the volume
     void Start () {
                 audio.Play ();
                     }
     void OnGUI () {
         Application.LoadLevel (1);// Starting the menu...
         DontDestroyOnLoad(transform.gameObject); //$$anonymous$$eep the camera because i want the music to continoue after changing scenes
         level = Application.loadedLevel;
         if (level == 1)
         {
             GUI.Box(new Rect(sx_box,sy_box,ex_box,ey_box), "ICT");
             if(GUI.Button(new Rect(sx1,sy1,ex1,ey1), "Start")) {
             Application.LoadLevel(3);//Start the game
             }
             if(GUI.Button(new Rect(sx2,sy2,ex2,ey2), "Options")) {
                 Application.LoadLevel(2);//Options
             }
             }
         if (level == 2)
         {
             x = GUI.HorizontalSlider(new Rect(625, 90, 150, 20), x, 0.0F, 10.0F);
             audio.volume = x;
             if(GUI.Button(new Rect(sx2,sy2,ex2,ey2), "Back")) {
                 Application.LoadLevel(1);//Back to the menu
             }
         }
     }
 }

avatar image Nerevar · Jul 09, 2014 at 05:20 PM 0
Share

As i Said, OnGUI is called several times per frame so:

  Application.LoadLevel (1);// Starting the menu...
     DontDestroyOnLoad(transform.gameObject); 

this should also be in start or you reload level 1 all the time

avatar image Sipi2000 · Jul 10, 2014 at 05:37 AM 0
Share

Okay thanks just one question:Why are you so clever??

avatar image Nerevar · Jul 10, 2014 at 09:19 AM 0
Share

Lol, it is just experience in programmation and with unity that speaks. Nothing extraordinary I suppose, I don't know :p

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Nerevar · Jul 09, 2014 at 01:34 PM

Hello

within this situation audio.Play(); should be in a Start() function

OnGUI is called several times per frame so you constantly replay the sound from the start :p

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

2 People are following this question.

avatar image avatar image

Related Questions

How to create sound on keyboard button? 1 Answer

Distribute terrain in zones 3 Answers

How to make Audio "Range" Further. 2 Answers

Play audio from directory? 0 Answers

Multiple Cars not working 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