- Home /
After the help from the people below, some soul searching through google and ultimately this tutorial video - https://www.youtube.com/watch?v=uw0kZ72zCvE I was able to resolve the issue
PlayerPrefs to save setActive state
Hi,
I have some script that is run from my Game scene which hides a gameObject on my Menu scene. However each time the game is closed and reopened the gameObject in my Menu scene is set back to active. I want the gameObject that's been hidden to remain hidden, even through a game reload. This is the code that I have at the moment.
DontDestroyOnLoad(gameObject);
SceneManager.LoadScene("sceneSelectBeta");
GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("Level2Block");
foreach(GameObject go in gameObjectArray) {
go.SetActive (false);
PlayerPrefs.????? ("?????", ?????);
}
DestroyObject(gameObject);
I use PlayerPrefs to save other information in my project, but I'm not sure how to implement it in this case, any help or advice would be much appreciated.
Answer by Mukabr · Aug 17, 2016 at 07:52 PM
Try using this so you can save arrays of booleans and than set/get.
I'm aware of the different functions of the PlayerPrefs, but I'm not sure of a way to implement them in my current code.
$$anonymous$$eep in $$anonymous$$d that the link that I posted is not the same as the Unity's PlayerPrefs. About the aplication, you could do something like this to save the active state
void Start () {
DontDestroyOnLoad(gameObject);
Scene$$anonymous$$anager.LoadScene("sceneSelectBeta");
GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag("Level2Block");
bool[] isActive = new bool[gameObjectArray.Length];
foreach (GameObject go in gameObjectArray)
{
go.SetActive(false);
isActive[i] = false;
}
PlayerPrefsX.SetBoolArray("Level2Block", isActive);
DestroyObject(gameObject);
}
And than something like this to get the active state
GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag("Level2Block");
foreach (GameObject go in gameObjectArray)
{
go.SetActive(PlayerPrefsX.GetBoolArray("Level2Block")[i]);
}
Thanks for the suggestion, I haven't been able to get this working in my project at the moment, I'm adding the first block of code to the start function of my game scene and getting 2 errors 'The name 'i' does not exist in the current content' and 'The name 'PlayerPrefsX' does not exist in the current context.
I'm also unsure where the second block of code should be put, should it be added to the game scene also, or should it be put in the menu scene? Thanks for the help you've given.
Answer by g__l · Aug 18, 2016 at 03:36 PM
In player prefs you can only save three types of variable int, float and string. In order to save a bool i would suggest saving it as an int where if that variable is 1 the state is true and 0 it is false.
(An Example)
To set an int use PlayerPrefs.SetInt("Active" , 0)
Then to get it use PlayerPrefs.GetInt("Active")
which returns the value.
A link to the documentation - https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
If you require any further assistance just ask, I hope that helps.
Thanks @g_l for the help. This is the code at the moment, it is able to compile but it still is setting the object back to active after reloading the game.
PlayerPrefs.SetInt ("Set Active", 1);
DontDestroyOnLoad(gameObject);
GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("Level2Block"); //find game object with tag
foreach(GameObject go in gameObjectArray) {
go.SetActive (false);
bool active = PlayerPrefs.GetInt("Set Active" , 1) == 0;
}