Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by xerxes78 · Jun 07, 2017 at 06:38 PM · javascriptandroid buildplayerprefssave data

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];
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by rafaeltavares · Nov 10, 2017 at 12:37 PM

Your start function bugs me...

function Start () { ChangeTexture(ChosenColor); }

Would do the same thing...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

147 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Are playerprefs a safe way of saving data or not?? 2 Answers

How to save a value in player prefs? Var. (java) 0 Answers

How do I save a previously bought characters in my Game? 0 Answers

Keeping track of player choices/actions? 0 Answers

Player prefs confusion + high score display (Java) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges