- Home /
Scene selection menu
I have been trying to create a game on Unity in these past few weeks. The game is to be for a school project and it's not gonna be anything big but since I'm quite a newbie on Unity and c# I've had my troubles, which were all mostly cleared until now...
I'm on the final phase of my project and all I need right now is a working menu that allows the user to choose from the 3 scenes in the game and start one of them.
What I did here was to create a Canvas with 3 buttons, each of them with the following scripts depending on the level desired:
using UnityEngine;
using System.Collections;
public class loadlevelX : MonoBehaviour {
public void LoadStage() {
Application.LoadLevel ("LvDesired");
}
}
I also tested with void Update() and void Start(), same result or no result alone, if necessary I'll explain in more detail.
My problem is that each time I start the scene (with all scenes already added to the project of course) I can choose any button but they all load the same level even if all the buttons have different scripts according to the level.
Can someone explain to me what am I doing wrong? Every single video I find is either a 1 level opening menu with just "Start Game" or "Exit" or is plain outdated (pre Unity 4.6)...
Are you using the new 4.6 UI buttons or OnGUI buttons. If the new ones for a project this simple just create 3 public functions that each only load 1 level then link the onClick event to the right function for that button.
That worked! For future reference to anybody who might have a similar problem, what I did was basically change the code to:
using UnityEngine;
using System.Collections;
public class loadlevel : $$anonymous$$onoBehaviour {
void OnClick(){
LvXStart ();
}
public void LvXStart() {
Application.LoadLevel ("LvX");
}
}
and on the OnClick part on the button I added the button to be used and on the function the loadlevel.LvXStart()
That solved my problem, thank you very much!
Answer by Firedan1176 · Mar 11, 2015 at 05:19 PM
You could also try to create a public string on your script and type in the name of your level in the inspector. Then it's different for each button.
I'm confused as to why you'd use OnClick() when you can just use Unity's button system. On the button there should be an OnClick() section, and you don't need it in code. You could do something like this:
void StartALevel(string levelName) {
Application.LoadLevel(levelName);
}
And in the inspector:
In the OnClick() section, you can click the +, drag the gameObject with the script into the empty field, then use the dropdown list to select your LoadLevel script, and then the StartALevel() function. There will be a space where you can then enter in a string (which is why my example says string levelName
), which can be different for each button.