Application.LoadLevel won't work

hi everyone

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");

        }
           


    }


}

Can you be more specific? Doesn’t work doesn’t really say much. Do the log statements get printed? Do you get any errors?

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

The code looks fine. Are you 100% sure that the names that you check in the if statements are correct? Also the upper/lower case?

OK, this means the problem is with your if statements. The if test doesn’t become true, and the code inside is never executed.

I’m confused by your use of global, what is this?

global is another script attached to an empty gameobject

here it is

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class global : MonoBehaviour {
    #region attributes
   
    public static Transform Cursor;
    public static Transform Selected_cursor;
    public static TextMesh Player_name;
    public static string CurrentPlayer;
    public static  Transform bouton;



    #endregion attributes
   
   
    // Use this for initialization
    void Awake () {
       
        DontDestroyOnLoad (gameObject);
       
       
        Cursor = GameObject.Find("Cursor").transform;
        Selected_cursor = GameObject.Find ("Selected_cursor").transform;
        Player_name = GameObject.Find ("Player_name").GetComponent<TextMesh> ();
        bouton = GameObject.Find ("Canvas").transform;
       
       
        Cursor.GetComponent<Renderer>().enabled = false;
        Selected_cursor.GetComponent<Renderer>().enabled = false;
       
    }





}
public enum ActionType {none, StartGame}

I don’t know why you are using static variables like that. I mean you shouldn’t.

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.

i’ve used CompareTo ,i’ve changed global and it still doesn’t work

when i delete the static statement an error shows saying that [quote]
An object reference is required to access non-static member
[/quote]

Since variable is no longer static you can’t access it. You need reference.

Here is a great example on how to reference non static variable from another script(how to access it).

But I doubt this will solve your main problem.

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.

as i selects a character and presses on the button
the Debug.Log (global.CurrentPlayer);

shows the correct information about the chosen character, but it wouldn’t compare. it’s as simple as 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.

it worked :smile:, i had to add a space to the string i have to compare to

like this global.CurrentPlayer.CompareTo("old ")

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.