Help required with basic PlayPrefs . Please :)

At the moment I have two scenes set up , I will need to set up more obviously but for now im trying to set up my language selection screen , what im trying to do here . Is basically a player will select the language of choice on the first time of running the game , from then on I want it to remember the choice and move into the next scene without having to chose the language again

    public  int  Language   ;
     
    void Start() {
    Language = PlayerPrefs.GetInt ("myLanguage",-1);
     
    if(Language == -1)
     {
     Application.LoadLevel (0);
     }
        if(Language == 2)
        {
        Application.LoadLevel(1);
        }
     
     
    IEnumerator playSoundThenLoad()
    {
    audio.Play ();
    yield return new WaitForSeconds (audio.clip.length);
    PlayerPrefs.Save ();
    Application.LoadLevel (1);
    }
     
     
    Void OnGUI ()
     
    {
     
    if (Application.loadedLevel == 0 )  
    {
    GUI.DrawTexture (new Rect(Screen.width/350f,Screen.height/500f,Screen.width/1f,Screen.height/1f),languageScreen);
    GUI.DrawTexture (new Rect(Screen.width/1000f,Screen.height/2000f,Screen.width/1f,Screen.height/14f),MainMenuButtonBar);
               
    GUI.skin.button = englishLanguageSelectionButton;
    if ( GUI.Button (new Rect (Screen.width /3.2f, Screen.height /2.8f, Screen.width / 2.5f, Screen.height / 4.4f), ""))
    {
    PlayerPrefs.SetInt("myLanguage",Language);
    Language = 2;
    StartCoroutine(playSoundThenLoad());
    English   = true;
    Chinese   = false;
    Spanish   = false;
    StartCoroutine(playSoundThenLoad());
     
    }
    GUI.skin.button = chineseLanguageSelectionButton;
    if (GUI.Button (new Rect(Screen.width/3.2f,Screen.height/1.83f,Screen.width / 2.5f, Screen.height / 4.4f),""))
    {
    StartCoroutine(playSoundThenLoad());
    English   = false;
    Chinese   = true;
    Spanish   = false;
    PlayerPrefs.SetInt("myLanguage",Language);
    Language = 3;
    }
     
    GUI.skin.button = spanishLanguageSelectionButton;
    if (GUI.Button (new Rect (Screen.width / 3.2f, Screen.height /1.35f, Screen.width / 2.5f, Screen.height / 4.4f), ""))
    {
    StartCoroutine(playSoundThenLoad());
    English   = false;
    Chinese   = false;
    Spanish   = true;
    PlayerPrefs.SetInt("myLanguage",Language);
    Language = 4;
    }
    }
     
     
    }

When you GetInt() you need to store the integer somewhere.

PlayerPrefs.GetInt("myLanguage");
// Change to
Language = PlayerPrefs.GetInt ("myLanguage", 1);
// The "1" means default to 1 if no int has been saved yet.

Thanks for the reply Garth , i tried you method and and I still cannot get the prefs to work , basically I have a language selection scene and I only want this to display for the first time a user loads the game after that I want the game to load into the main menu and skip the language scene but remember what the player has selected.

Ive changed my code a good bit but I cannot get it to work , I know im making a mess somewhere

void OnApplicationQuit() 
{
PlayerPrefs.Save ();
}

	void Start()
	{
		if (Language == PlayerPrefs.GetInt ("myLanguage", 1)) {
			Application.LoadLevel (0);
		}
		if (Language == PlayerPrefs.GetInt ("myLanguage", 2)) {
			Application.LoadLevel (1);
		}
	}

and In the GUI function …

GUI.skin.button = englishLanguageSelectionButton;
if ( GUI.Button (new Rect (Screen.width /3.2f, Screen.height /2.8f, Screen.width / 2.5f, Screen.height / 4.4f), ""))
{
PlayerPrefs.SetInt("myLanguage",Language);
Language = 2;
StartCoroutine(playSoundThenLoad());
English   = true;
Chinese   = false;
Spanish   = false;
StartCoroutine(playSoundThenLoad());

that cant work …
do somethik like

// get the int with the key "myLanguage", if there isnt one returns -1
Language == PlayerPrefs.GetInt ("myLanguage", -1);

if(Language == -1)
{
    // choose a language
}
else
{
     Application.LoadLevel( Language  )
}

Assign with the single =, not the double.

Language = PlayerPrefs.GetInt ("meLanguage",-1);

Thanks for the feed back Zaladur ,
i tried your method but it does not seem to work

oid Start()
{
Language = PlayerPrefs.GetInt ("myLanguage",-1);

if(Language == -1)
{
			
Application.LoadLevel (0);
			
}
		
else
			
{
			
Application.LoadLevel(1);
				
}

this will not let any selection presses load the next menu level

This is driving me mad , I honestly cant see where I going wrong , if anyone can help you will be my hero for life !! lol heres the lines of code Im using

public  int  Language   ;

void Start() {
Language = PlayerPrefs.GetInt ("myLanguage",-1);

if(Language == -1)
 {
 Application.LoadLevel (0);
 }
    if(Language == 2)
    {
    Application.LoadLevel(1);
    }


IEnumerator playSoundThenLoad() 
{ 
audio.Play ();
yield return new WaitForSeconds (audio.clip.length);
PlayerPrefs.Save ();
Application.LoadLevel (1);
}


Void OnGUI ()

{

if (Application.loadedLevel == 0 )  
{
GUI.DrawTexture (new Rect(Screen.width/350f,Screen.height/500f,Screen.width/1f,Screen.height/1f),languageScreen);
GUI.DrawTexture (new Rect(Screen.width/1000f,Screen.height/2000f,Screen.width/1f,Screen.height/14f),MainMenuButtonBar);
			
GUI.skin.button = englishLanguageSelectionButton;
if ( GUI.Button (new Rect (Screen.width /3.2f, Screen.height /2.8f, Screen.width / 2.5f, Screen.height / 4.4f), ""))
{
PlayerPrefs.SetInt("myLanguage",Language);
Language = 2;
StartCoroutine(playSoundThenLoad());
English   = true;
Chinese   = false;
Spanish   = false;
StartCoroutine(playSoundThenLoad());

}
GUI.skin.button = chineseLanguageSelectionButton;
if (GUI.Button (new Rect(Screen.width/3.2f,Screen.height/1.83f,Screen.width / 2.5f, Screen.height / 4.4f),""))
{
StartCoroutine(playSoundThenLoad());
English   = false;
Chinese   = true;
Spanish   = false;
PlayerPrefs.SetInt("myLanguage",Language);
Language = 3;
}

GUI.skin.button = spanishLanguageSelectionButton;
if (GUI.Button (new Rect (Screen.width / 3.2f, Screen.height /1.35f, Screen.width / 2.5f, Screen.height / 4.4f), "")) 
{
StartCoroutine(playSoundThenLoad());
English   = false;
Chinese   = false;
Spanish   = true;
PlayerPrefs.SetInt("myLanguage",Language);
Language = 4;
}
}


}

please anyone :slight_smile:

bump

Anyone :slight_smile: still stuck with this one

Hey this is what I got.

Something I noticed when testing this though… my clicks on the GUI Button didn’t always register. And the buttons were overlapping a little. I’m not too familiar with Unity’s GUI so I don’t know why that’s happening.

I had to comment some stuff out because I didn’t have all the variables.

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
    
    void Start ()
    {
        int Language = PlayerPrefs.GetInt (_languageKey, -1);
        if (Language == -1) {
            Application.LoadLevel (0);
        }
        else {
            Application.LoadLevel (1);
        }
    }
    
    IEnumerator playSoundThenLoad ()
    {
        Debug.Log ("[" + Time.time + "] Coroutine running! Will load level 1");
        //audio.Play ();
        //yield return new WaitForSeconds (audio.clip.length);
        PlayerPrefs.Save ();
        Application.LoadLevel (1);
        yield return null;
    }

    void OnGUI ()
    {
        if (Application.loadedLevel == 0) {
//            GUI.DrawTexture (new Rect (Screen.width / 350f, Screen.height / 500f, Screen.width / 1f, Screen.height / 1f), languageScreen);
//            GUI.DrawTexture (new Rect (Screen.width / 1000f, Screen.height / 2000f, Screen.width / 1f, Screen.height / 14f), MainMenuButtonBar);
            
//            GUI.skin.button = englishLanguageSelectionButton;
            if (GUI.Button (new Rect (Screen.width / 3.2f, Screen.height / 2.8f, Screen.width / 2.5f, Screen.height / 4.4f), "")) {
                Debug.Log ("English");
                PlayerPrefs.SetInt (_languageKey, 2);
                StartCoroutine (playSoundThenLoad ());
                
            }
//            GUI.skin.button = chineseLanguageSelectionButton;
            if (GUI.Button (new Rect (Screen.width / 3.2f, Screen.height / 1.83f, Screen.width / 2.5f, Screen.height / 4.4f), "")) {
                Debug.Log ("Chinese");
                PlayerPrefs.SetInt (_languageKey, 3);
                StartCoroutine (playSoundThenLoad ());
            }
            
//            GUI.skin.button = spanishLanguageSelectionButton;
            if (GUI.Button (new Rect (Screen.width / 3.2f, Screen.height / 1.35f, Screen.width / 2.5f, Screen.height / 4.4f), "")) {
                Debug.Log ("Spanish");
                PlayerPrefs.SetInt (_languageKey, 4);
                StartCoroutine (playSoundThenLoad ());
            }
        }
    }

    bool English { get { return PlayerPrefs.GetInt(_languageKey, -1) == 2; } }
    bool Chinese { get { return PlayerPrefs.GetInt(_languageKey, -1) == 3; } }
    bool Spanish { get { return PlayerPrefs.GetInt(_languageKey, -1) == 4; } }

    const string _languageKey = "myLanguage";
}

Gareth thank you for that and really I mean it , i think im going to just give up on this , Ive tried so many methods and I tried yours , it selects the language and loads level 1 like i need it to but it appears to stick in a loop and keeps loading level 1 over and over so I cannot use any of the gui functions etc for more than a split second without it reloading the level , I have it added to the start function and It appears to keep running its baffling , but really though thank you for that , it will help me in future , its a really neat way of using a playerpref

:slight_smile:

Start is going to run on this script whenever a level loads and this script is present. So if you have this script on an object in every scene then that would explain the looping behavior you experienced.

Honestly - pare your code down. Start with the default GUI and get it working without using PlayerPrefs. Just seems like you tried to do too much at once.

Thanks KelsoMRK , I thought buy just adding application.loadedlevel == (0) inside the if statement would stop it from calling the statement in the start function on the second level but it did not . So thanks for that and thanks to Gareth I cleaned up the script and made a second script for just that language selection screen , probably would have worked for me 2 weeks ago if I had tried that … BUT THERE IS STILL A PROBLEM !!! lol , THE new script is working and saving the selection how I want it to and loading level 1
however when I build this out on mobile it just sits and will not do anything at all , I have a gui skin so i can see the gui buttons are being pressed or it is at least reading the button press but it will not load level 1 it stick on that screen , when i remove the start function it removes the save ability but it will load level 1 , but that puts me back to square one , I know I nearly have it working but why would it not work on mobile ??

using UnityEngine;
using System.Collections;

public class LanguageScreenGUI : MonoBehaviour {

	public  int  Language  ;

	bool English { get { return PlayerPrefs.GetInt(_languageKey, -1) == 2; } }
	bool Chinese { get { return PlayerPrefs.GetInt(_languageKey, -1) == 3; } }
	bool Spanish { get { return PlayerPrefs.GetInt(_languageKey, -1) == 4; } }

	
	const string _languageKey = "myLanguage";

	public GUIStyle englishLanguageSelectionButton;
	public GUIStyle chineseLanguageSelectionButton;
	public GUIStyle spanishLanguageSelectionButton;
	public Texture2D languageScreen;
	public Texture2D MainMenuButtonBar;
	public AudioClip sfxButton;


	void Start ()
		
	{
		//PlayerPrefs.DeleteAll ();

	int Language = PlayerPrefs.GetInt (_languageKey, -1);
		
		if (Language == -1) 
		{
			
			Application.LoadLevel (0);
			
		}
		
		else {
			
			Application.LoadLevel (1);
			
		}
		
	}


	void Update()
	{

		if (Input.GetKeyDown (KeyCode.Escape)  Application.loadedLevel == 0) 
		{
			PlayerPrefs.Save ();
			audio.Play ();
			Application.Quit();
		}

	}

	IEnumerator playSoundThenLoad() 
	{ 
		//audio.Play ();
		//yield return new WaitForSeconds (audio.clip.length);
		PlayerPrefs.Save ();
		Application.LoadLevel (1);
		yield return null;
	}
	

	void OnGUI () {

		if (Application.loadedLevel == 0 )  
		{
			GUI.DrawTexture (new Rect(Screen.width/350f,Screen.height/500f,Screen.width/1f,Screen.height/1f),languageScreen);
			GUI.DrawTexture (new Rect(Screen.width/1000f,Screen.height/2000f,Screen.width/1f,Screen.height/14f),MainMenuButtonBar);
			
			GUI.skin.button = englishLanguageSelectionButton;
			if ( GUI.Button (new Rect (Screen.width /3.2f, Screen.height /2.8f, Screen.width / 2.5f, Screen.height / 4.4f), ""))
			{
				Debug.Log ("English");
			
				PlayerPrefs.SetInt (_languageKey, 2);
				
				StartCoroutine (playSoundThenLoad ());
				
			}
		
			GUI.skin.button = chineseLanguageSelectionButton;
			if (GUI.Button (new Rect(Screen.width/3.2f,Screen.height/1.83f,Screen.width / 2.5f, Screen.height / 4.4f),""))
			{
				 
					
					Debug.Log ("Chinese");

				PlayerPrefs.SetInt (_languageKey, 3);
					
				   StartCoroutine (playSoundThenLoad ());
			}
			
			GUI.skin.button = spanishLanguageSelectionButton;
			if (GUI.Button (new Rect (Screen.width / 3.2f, Screen.height /1.35f, Screen.width / 2.5f, Screen.height / 4.4f), "")) 
			{
					Debug.Log ("Spanish");
			
				PlayerPrefs.SetInt (_languageKey, 4);
					
					StartCoroutine (playSoundThenLoad ());
			}
		}
	
	}

}

If you’ve previously saved something with PlayerPrefs on device then there is a good chance those values are still there and being loaded.

Ive tried building out with a version including playerprefs.DeleteAll(); in the start function so its not that ,

when I line out the Start statement below , the game will run through the options as per normal but obviously is wont store/save any selections the GUI buttons work fine.

void Start (){

//int Language = PlayerPrefs.GetInt (_languageKey, -1);
		
	//if (Language == -1) 
	//{
			
		//Application.LoadLevel (0);
			
		//}
		
		//else {
			
		//	Application.LoadLevel (1);
			
		//}
		
	}

When the Update function is used in the Unity player or PC build it works correctly however the buttons seem to need more than one or two presses to load continue and load the next level. However I have noticed that although the gui buttons somewhat work on PC but do not carry out functions on the Mobile although the press is registered , the Exit function I have added will not work on either PC or Mobile on that language screen when the update function is being used , If I remove the update function for the playerprefs it works just fine

void Update()
	{

		if (Input.GetKeyDown (KeyCode.Escape)  Application.loadedLevel == 0) 
		{
			PlayerPrefs.Save ();
			audio.Play ();
			Application.Quit();
		}

I believe you cannot “Exit” on mobile devices. In fact, apps that can “exit” fail the app approval process.

The other part about PlayerPrefs.DeleteAll() is that if you put it in the Start() function, then you never save your Language setting because it just gets deleted right away.

Meanwhile, without DeleteAll(), if you have picked a language then the Choose Language scene will never appear again because a Language has already been set. What’s missing here is some way for a player to say “I want to change my picked language.”

I’m going to concur with KelsoMRK who said, “Start with the default GUI and get it working without using PlayerPrefs.” In our games we don’t usually even bother with saving data until we have a game that’s worth playing.

The "Exit function on a mobile device is always required for App submission , its the mobile back button that must return you to a the previous screen or within the main menu or initial screen is must close the app or pop up an exit check yes / no box . That`s all working fine but it becomes disabled with the player prefs.

The GUI is basically finished all working perfectly well with no issues all the menus and pop up all the in game options for the various levels etc are all in place and the game is 70 percent complete from a gameplay point of view , adding the PlayerPrefs is the next stage Im working on here , adding score and coin saves and shop purchases I pretty much have in order and working but need various custom textures created etc , the only thing I cannot get working is the language selection screen , which I would much rather not have in he first place however as this game will be launched and published by a 3rd party for the Chinese market we/I need to add language options , I have hired two people to translate the various text within the game and to provide voice overs , which they are finished with and Im trying to get everything together and tested so I can sign off on that part of the build , I ve never tried to start a game or application on its initial load using player prefs to determine anything so this is were I am at a loss , thats to your script I can clean up the other prefs I have as your way is a lot easier to use .
The GUI I have decided on is to create a clean menu and game screen while in portrait mode which is harder than it sounds as space is a premium , so it involves a lot of branching and pop up buttons but it keeps everything simple and very easy to use and the draw calls down as each button or group of buttons disable the previous buttons , when I have it complete and working correctly I will be putting the build (GUI Part) up here for everyone to use at there own pleasure , The full GUI consists of 4 Scripts amounting to nearly 1300 lines of code . The game play consists of single and multiplayer modes with the main game mode consisting of an initial 40 levels but that is being monitored with the available space we have on the final build due to Android restrictions .

So you see basically everything is where it needs to be , (of course there`s always something else to be done , there always is ) but as we have now under 3 weeks to provide a working demo of the game this is in my top priorities right now . Thanks again for the scripts yesterday it really will help

The only reason I mentioned the Exit / back function was to point out that it works along with the buttons within the language screen when I not saving the playerPrefs , however it becomes disabled when I try and save the prefs , so Im thinking along the lines of what ever is causing that is the same issue with the GUI not working correctly .

It feels like the function is being called in the Update function and while there is no language selection is keeps looping and reloading the level which is shouldnt while inside an Start function , I tried to wrap in in an Awake function but that just crashes the build

FFS << is all i have to say about this , Our last build on Windows Mobile and the one previous we had one simple but hard to spot issue and here it is again and yet I still did not look at it as an issue . And Ive ran tests on the very first playerpref attempt I made on this over two weeks ago before and it works perfectly , So guys honestly before I beat myself with a bat thanks for all your input on this one , and Garth thank you for the script , I never seen or thought of using playerprefs most notably the bools , that will clean up a lot of my scripts or well break them all as im sure to mess up trying to replace them.

Anyway , I still have not got to the cause of the Exit/Back button not registering , it wont give me so much as a debug message and its only on that scene/level everything else brings up our exit/stepback check options box . If I figure it out ill post it here .

Here is the script im using now working (with the back / escape etc button mystery)

using UnityEngine;
using System.Collections;

public class LanguageScreenGUI : MonoBehaviour {

	public  int  Language  ;

	bool English { get { return PlayerPrefs.GetInt(_languageKey, -1) == 2; } }
	bool Chinese { get { return PlayerPrefs.GetInt(_languageKey, -1) == 3; } }
	bool Spanish { get { return PlayerPrefs.GetInt(_languageKey, -1) == 4; } }

	
	const string _languageKey = "myLanguage";

	public GUIStyle englishLanguageSelectionButton;
	public GUIStyle chineseLanguageSelectionButton;
	public GUIStyle spanishLanguageSelectionButton;
	public Texture2D languageScreen;
	public Texture2D MainMenuButtonBar;
	public AudioClip sfxButton;

	#region languagePrefs
	void Start()
		
	{

		int Language = PlayerPrefs.GetInt (_languageKey, -1);

		if (Language == -1) {
			
			Application.LoadLevel (0);
			
		}
		
		else {
			
			Application.LoadLevel (1);
			
		}
		
	}
	#endregion

	void Update()
	{

		if (Input.GetKeyDown (KeyCode.Escape) ) 
		{

			PlayerPrefs.SetInt (_languageKey, 2);
			Application.Quit();
		}

	}

	IEnumerator playSoundThenLoad() 
	{ 
		audio.Play ();
		yield return new WaitForSeconds (audio.clip.length);
		PlayerPrefs.Save ();
		Application.LoadLevel (1);
		yield return null;
	}
	

	void OnGUI () {

		if (Application.loadedLevel == 0 )  
		{
			GUI.DrawTexture (new Rect(Screen.width/350f,Screen.height/500f,Screen.width/1f,Screen.height/1f),languageScreen);
			GUI.DrawTexture (new Rect(Screen.width/1000f,Screen.height/2000f,Screen.width/1f,Screen.height/14f),MainMenuButtonBar);
			
			GUI.skin.button = englishLanguageSelectionButton;
			if ( GUI.RepeatButton (new Rect (Screen.width /3.2f, Screen.height /2.8f, Screen.width / 2.5f, Screen.height / 4.4f), ""))
			{
				Debug.Log ("English");
			
				PlayerPrefs.SetInt (_languageKey, 2);
				PlayerPrefs.Save ();
				Application.LoadLevel (1);
				
			}
		
			GUI.skin.button = chineseLanguageSelectionButton;
			if (GUI.RepeatButton (new Rect(Screen.width/3.2f,Screen.height/1.83f,Screen.width / 2.5f, Screen.height / 4.4f),""))
			{
				 
					
					Debug.Log ("Chinese");

				PlayerPrefs.SetInt (_languageKey, 3);
				PlayerPrefs.Save ();
				Application.LoadLevel (1);
			}
			
			GUI.skin.button = spanishLanguageSelectionButton;
			if (GUI.RepeatButton (new Rect (Screen.width / 3.2f, Screen.height /1.35f, Screen.width / 2.5f, Screen.height / 4.4f), "")) 
			{
					Debug.Log ("Spanish");
			
				PlayerPrefs.SetInt (_languageKey, 4);
				PlayerPrefs.Save ();
				Application.LoadLevel (1);
     }
    }
  }
}

The issue ?? With the increased touch sensitivity of windows Mobile over other devices it can actually be a problem for GUI buttons , something I have already known about and had issues with in the past and yet still did not cop-on

	                GUI.skin.button = englishLanguageSelectionButton;
			if ( GUI.Button (new Rect (Screen.width /3.2f, Screen.height /2.8f, Screen.width / 2.5f, Screen.height / 4.4f), ""))
			{
				Debug.Log ("English");
			
				PlayerPrefs.SetInt (_languageKey, 2);
				PlayerPrefs.Save ();
				Application.LoadLevel (1);
				
			}

Using the normal GUI.Button function for what ever reason does , in most cases , will not carry out the function , an issue I only have found with windows , simply using GUI.RepeatButton like below is the solution … and i a complete idiot of letting this go on and on and on

			GUI.skin.button = englishLanguageSelectionButton;
			if ( GUI.RepeatButton (new Rect (Screen.width /3.2f, Screen.height /2.8f, Screen.width / 2.5f, Screen.height / 4.4f), ""))
			{
				Debug.Log ("English");
			
				PlayerPrefs.SetInt (_languageKey, 2);
				PlayerPrefs.Save ();
				Application.LoadLevel (1);
				
			}

gave Tic Toc Games and fractured state a following on the old facebook :slight_smile: