So I'm working with a customizer for my snake game and I am using PlayerPrefs to save the preference look of the snake. Now everytime I test-run the game it the saved string in playerprefs doesn't load it's just empty.
I've already tried getting the string data on the Start(), Awake(), and Update() method and none of them loads the data. Also I've add PlayerPrefs.Save() in each line I save the data.
Here's my code for updating all mesh to be the same as the saved string. I've put it in the Update Method.
lesnek = GameObject.FindGameObjectsWithTag("LeWorm");
objName = PlayerPrefs.GetString("meshName");
if (PlayerPrefs.HasKey("meshName"))
{
if (objName == PlayerPrefs.GetString("meshName"))
{
leMesh = Resources.Load<GameObject>(@"Models\" + objName);
}
else
{
objName = PlayerPrefs.GetString("meshName");
leMesh = Resources.Load<GameObject>(@"Models\" + objName);
}
}
if (objName != null || objName != "")
{
objName = "Sphere";
leMesh = Resources.Load<GameObject>(@"Models\" + objName);
}
foreach (GameObject change in lesnek)
{
if (objName != null || objName != "")
{
if (change.GetComponent<MeshFilter>() == null)
{
change.AddComponent<MeshFilter>();
change.GetComponent<MeshFilter>().mesh = leMesh.GetComponent<MeshFilter>().sharedMesh;
PlayerPrefs.SetString("meshName", objName);
PlayerPrefs.Save();
}
else
{
change.GetComponent<MeshFilter>().mesh = leMesh.GetComponent<MeshFilter>().sharedMesh;
PlayerPrefs.SetString("meshName", objName);
PlayerPrefs.Save();
}
}
}
Every time I run this code I get an error and doesn't load or save the string in the playerprefs.