- Home /
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);
}
}
}
}
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
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
}
}
}
}
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
Lol, it is just experience in programmation and with unity that speaks. Nothing extraordinary I suppose, I don't know :p
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
Your answer
Follow this Question
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