Playerprefs not saving on Android but saving in unity run editor
Can someone take a look at this code and see what I am doing wrong? In unity when running the game it will change textures and save them from scene to scene no problems. But when I build for android and test it on my phone, the textures are not being saved when changed.
Any help would be great! Ive been searching for an answer on this for days and have tried all I can find with no luck.
#pragma strict
// Make the script also execute in edit mode.
@script ExecuteInEditMode()
var ChosenColor : int; //the playerPrefs integer for saving the number that represents the chosen color.
var savedDecal : int = 0; //the playerPrefs integer for saving our decal that was chosen.
var vehicle01 : Renderer; //the object with the material on it that contains the texture we want to change.
//var vehicle02 : Renderer;
public var textures : Texture2D[]; // the array holding all of the possible textures for the vehicle.
//these are separate textures for the GUI buttons...they don't have an alpha channel.
var btnTextureGreen : Texture;
var btnTextureBlue : Texture;
var btnTextureRed : Texture;
var btnTextureYellow : Texture;
var btnTextureHotPink : Texture;
var btnTexturePurple : Texture;
var btnTextureGray : Texture;
var btnTextureTeal : Texture;
var btnTextureWhite : Texture;
function Start () {
if (ChosenColor == 0)
{
ChangeTexture(0);
}
if (ChosenColor == 1)
{
ChangeTexture(1);
}
if (ChosenColor == 2)
{
ChangeTexture(2);
}
if (ChosenColor == 3)
{
ChangeTexture(3);
}
if (ChosenColor == 4)
{
ChangeTexture(4);
}
if (ChosenColor == 5)
{
ChangeTexture(5);
}
if (ChosenColor == 6)
{
ChangeTexture(6);
}
if (ChosenColor == 7)
{
ChangeTexture(7);
}
if (ChosenColor == 8)
{
ChangeTexture(8);
}
}
function OnGUI() {
//these are the buttons for the color palette.
if (GUI.Button(Rect(10,140,64,32),btnTextureBlue))
{
ChangeTexture(0);
PlayerPrefs.SetInt("ChosenColor", 0);
PlayerPrefs.Save();
Debug.Log("ChosenColor = Blue");
}
if (GUI.Button(Rect(90,140,64,32),btnTextureRed))
{
ChangeTexture(1);
PlayerPrefs.SetInt("ChosenColor", 1);
PlayerPrefs.Save();
Debug.Log("ChosenColor = Red");
}
if (GUI.Button(Rect(170,140,64,32),btnTextureGreen))
{
ChangeTexture(2);
PlayerPrefs.SetInt("ChosenColor", 2);
PlayerPrefs.Save();
Debug.Log("ChosenColor = Green");
}
if (GUI.Button(Rect(250,140,64,32),btnTextureYellow))
{
ChangeTexture(3);
PlayerPrefs.SetInt("ChosenColor", 3);
PlayerPrefs.Save();
Debug.Log("ChosenColor = Yellow");
}
if (GUI.Button(Rect(330,140,64,32),btnTexturePurple))
{
ChangeTexture(4);
PlayerPrefs.SetInt("ChosenColor", 4);
PlayerPrefs.Save();
Debug.Log("ChosenColor = Purple");
}
if (GUI.Button(Rect(410,140,64,32),btnTextureTeal))
{
ChangeTexture(5);
PlayerPrefs.SetInt("ChosenColor", 5);
PlayerPrefs.Save();
Debug.Log("ChosenColor = Teal");
}
if (GUI.Button(Rect(490,140,64,32),btnTextureGray))
{
ChangeTexture(6);
PlayerPrefs.SetInt("ChosenColor", 6);
PlayerPrefs.Save();
Debug.Log("ChosenColor = Gray");
}
if (GUI.Button(Rect(570,140,64,32),btnTextureWhite))
{
ChangeTexture(7);
PlayerPrefs.SetInt("ChosenColor", 7);
PlayerPrefs.Save();
Debug.Log("ChosenColor = White");
}
if (GUI.Button(Rect(650,140,64,32),btnTextureHotPink))
{
ChangeTexture(8);
PlayerPrefs.SetInt("ChosenColor", 8);
PlayerPrefs.Save();
Debug.Log("ChosenColor = Hot Pink");
}
//Next row
if (GUI.Button(Rect(10,290,120,40), "Load Game Level"))
{
Application.LoadLevel ("UTV WIP");
}
// Change texture function
function ChangeTexture(colors : int)
{
// Just make a small check, that we don't try to grab a non-existing texture
if (colors >= textures.Length)
return;
// Change the material's texture
vehicle01.GetComponent.<Renderer>().sharedMaterial.mainTexture = textures[colors];
PlayerPrefs.Save();
//vehicle02.GetComponent.<Renderer>().sharedMaterial.mainTexture = textures[colors];
}
Answer by lsp1994 · Jul 05, 2017 at 03:48 PM
it's seem to be bug in uinty 5.6.1.f1 try to call PlayerPrefs.Save(); inside OnApplicationPause() callback.
for me i saved everything to file and load it when player back to game see this official tutorial Live Training 3 Mar 2014 - Data Persistence
Answer by rafaeltavares · Nov 10, 2017 at 12:37 PM
Your start function bugs me...
function Start () { ChangeTexture(ChosenColor); }
Would do the same thing...
Your answer
![](https://koobas.hobune.stream/wayback/20220612123721im_/https://answers.unity.com/themes/thub/images/avi.jpg)