- Home /
save game using player prefs
I have been looking around for a while have not been able to find an answer to my question, so I have decided to ask it my self.
Is there a way to use playerpref to across different worlds.
So I have a script on the main menu that is the following.
static var level : int;
function savelevel()
{
PlayerPrefs.SetInt("saveprog", level);
}
I can then use the following script to change the value.
PlayerPrefs.SetInt("saveprog",2);
I under stand this I think.
Any way is there a way for me to change this value while in another world, for instance, you finnish a level and then press save and continue. The script would then add another number to the saveprog value, which unlocks another level.
if you can not do this could some one please show me how.
thanks
Answer by Clarkmeister · Apr 24, 2016 at 07:31 AM
@O.R.C I know this is a really old post, and probably solved by now. But in case of someone stumbling across this page hopefully this will help.
PlayerPrefsGetInt would return the value that is saved in that tag or if that tag does not exist yet it would return the default value which is your second argument of 1, which is the default value for true. So if you would want to access the number stored in PlayerPrefs.GetInt("saveprog", 1); you would need something like (Switched To C#, Sorry):
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class LevelManager : MonoBehaviour
{
bool win;
void Update()
{
if(win)
{
SaveCurrentLevel(); //I would save the level in the Start() of the next scene though.
}
}
void SaveCurrentLevel() //Public if you want to call from a seperate script.
{
Scene currentScene = SceneManager.GetActiveScene();
PlayerPrefs.SetInt("PlayerProg", currentScene.buildIndex);
/*
* Or if you would like to save via Scene Name.
* PlayerPrefs.SetString("PlayerProg", currentScene.name);
*/
}
public void LoadCurrentLevel() //Call this when player hits 'P' or Play button etc.
{
SceneManager.LoadScene(PlayerPrefs.GetInt("PlayerProg", 1));
/*
* Or if you would like to load scene by name:
* SceneManager.LoadScene(PlayerPrefs.GetString("PlayerProg", "LevelOne"));
* */
}
}
Answer by fafase · Feb 19, 2014 at 06:17 AM
Well when you finish a level, you call the method:
PlayerPrefs.SetInt("saveprog",levelValue);
Then you get the value in the start of your game manager to know where you left off.
So you are suing that i attach the script you provided to the end of my level, and then in the main menu I have a script like this.
if(level == levelValue)
{
Application.Load("mylevel2");
}
If this is not right could you show me how? Thankd for your answer though, it is nice knowing that there are people helping.
i added your script to $$anonymous$$e but i get an error which says unknown identifier levelValue. how should i set up my script to stop this.
You need to look into Application.LoadLevel documentation. levelValue is either the name you gave to the level or the value it has in the build.
Also, you end check should be based on something happening in the game, either you need a certain amount of something or you reach a point or kill someone or else. Then that condition calls the next level.
I know how to call an Application.LoadLevel, the above is just a mistake which I have not fixed. I have been asking around on how to make the condition change. So I have made a value as you can see above, but i need to be able to change that value while I am not in the world that the value script is in. i have no clue on how to do this, and you can also see that i have no clue on how to call the value in the first place. If you know how to do ether, could you please show me.
Thanks
I have updated my script so it now looks like this.
This script is in level/mainmenu
#pragma strict
static var level : int;
function Update()
{
PlayerPrefs.SetInt("saveprog",level);
if(PlayerPrefs.GetInt("saveprog", 1))
{
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.P))
{
Application.LoadLevel(1);
}
}
}
Now this script is in level2/firstlevel
var youwin = false;
var levelValue = 1;
function Update()
{
if(youwin)
{
PlayerPrefs.SetInt("saveprog",levelValue);
}
}
what is wrong with this? it does not seam to work.