if(PlayerPrefs

using UnityEngine;
using System.Collections;

public class NextScene : MonoBehaviour {

    // Use this for initialization
    void Start () {
   
    if(PlayerPrefs.GetString("Scene", "Menu"))
    {
        Application.LoadLevel("Menu");
    }
    else
    {
        Application.LoadLevel("Login");
    }
   
    }
}

Error:

Assets/NextScene.cs(9,9): error CS0029: Cannot implicitly convert type `string' to `bool'

How to fix it?

You have to compare the result of PlayerPrefs.GetString to something. For example, change line 9 to

   if (PlayerPrefs.GetString("Scene", "Menu") == "Menu")
1 Like

But now its always loading Menu scene.
I deleted all player prefs and its still loading Menu scene.

Well, sure. You’re passing “Menu” as the default value (the second parameter to GetString), so if there are no player prefs, that’s what you get back.

If that’s not what you want, then don’t pass “Menu” as the default.

Hmm… I really don’t get it.
I want to do Login Scene as default, but when u open Menu scene, menu scene is going to be default.

In menu scene i have script

using UnityEngine;
using System.Collections;

public class SetStartScene : MonoBehaviour {

    // Use this for initialization
    void Start () {
        PlayerPrefs.SetString("Scene", "Menu");
    }
}

Then you do not want to pass in a 2nd parameter (the default value).

if (PlayerPrefs.GetString ("Scene") == "Menu") {

}
1 Like