i’m creating a character select menu for my game in unity, what i have for now is a UI Button that should load the next level with the chosen character. i’ve been able to get the character name whenever the button is pressed, however the Application.loadLevel() doesn’t work and i’ve added the scenes to the Build Settings
here’s my code :
using UnityEngine;
using System.Collections;
public class ActionOnButton : MonoBehaviour {
#region attributes
#endregion attributes
// Use this for initialization
void Start () {
GetComponent<CanvasGroup> ().interactable = false;
}
public void OnPress () {
Debug.Log ("clicked");
//Application.LoadLevel ("level1");
Debug.Log (global.CurrentPlayer);
if (global.CurrentPlayer == "old")
{
Debug.Log("old chosen");
Application.LoadLevel ("SalonOld");
}
if (global.CurrentPlayer == "Adult")
{
Debug.Log("adult choisit");
Application.LoadLevel ("SalonAdult");
}
if (global.CurrentPlayer == "younf")
{
Debug.Log("young choisit");
Application.LoadLevel("Salon");
}
//checkPlayer ();
}
void checkPlayer(){
if (global.CurrentPlayer == "Enfant") {
Debug.Log("enfant choisit");
Application.LoadLevel ("SalonOld");
} else if (global.CurrentPlayer == "Adulte") {
Debug.Log("adulte choisit");
Application.LoadLevel ("SalonAdult");
} else if (global.CurrentPlayer == "Enfant") {
Debug.Log("vieux choisit");
Application.LoadLevel("Salon");
}
}
}
i’m sorry fo not being too clear. the next level isn’t loaded and the current scene doesn’t change, i have no errors and the log statements inside the if statement isn’t printed also
global is a keyword in C# so you probably shouldn’t use that. Also when doing string comparisons, you should use CompareTo, and not == like so:
string a;
if (a.CompareTo("SomeText") == 0)
{
...
}
As @Nitugard says, it’s also not a good idea to use static variables like that.
None of these things are why your script is failing though, there is another error elsewhere. Probably the CurrentPlayer isn’t set correctly or something like that.
As I said, these things are not why your script is failing, but they are still considered wrong.
To figure out why your script is failing, you need to find out why your if tests never return true. Why for example global.CurrentPlayer never equals “Adult”. Maybe you’re never setting it to anything? Maybe you’re using wrong capitalisation and set it to “adult” instead? Maybe you’d be better off using an enum instead of a string, then you’d never have to worry about that.
I would suggest you try to find a different way to do it, that doesn’t involve comparing strings. Comparing strings is very prone to error.
If you really need to use strings, try to do something like Debug.Log(global.CurrentPlayer.CompareTo("old"));
Put this directly below your Debug.Log(global.CurrentPlayer);
If this prints out anything other than 0, then the strings are not identical.
Glad you got it working. I would however still recommend you try to find a way to do this without comparing strings. As you can see, it’s easy to get it wrong.