Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Save the Multiple name using the PlayerPrefs function

Discussion in 'Scripting' started by schetty, Mar 19, 2013.

  1. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    Hi,

    I have used below code for saving the multiple names using the array. But i have the issue in this. when i save the second name in array the first name also change. I dont know how to solve this, please help me to do this.

    Code (csharp):
    1. PlayerPrefs.SetString("JOKE"+no[0], "one");
    2.         PlayerPrefs.SetString("JOKE"+no[1], "two");
    3.        
    4.         GUI.Label(new Rect(10, 100, 100, 30), PlayerPrefs.GetString("JOKE"+no[0]));
    5.         GUI.Label(new Rect(10, 140, 100, 30), PlayerPrefs.GetString("JOKE"+no[1]));    
    Thanks.
     
  2. Lukas H

    Lukas H

    Joined:
    Jan 16, 2009
    Posts:
    394
    Please provide your full code. My guess would be that JOKE+no[0] and JOKE+no[1] produce the same key which would lead to JOKE+no[1] overrwriting JOKE+no[0].
     
  3. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    Code (csharp):
    1. public int[] no;
    2.     int i;
    3.     // Use this for initialization
    4.     void Start () {
    5.        
    6.         i=2;   
    7.     }
    8.    
    9.     // Update is called once per frame
    10.     void Update () {
    11.    
    12.     }
    13.     void OnGUI()
    14.     {  
    15.         PlayerPrefs.SetInt("No", no[0]);       
    16.         PlayerPrefs.SetString("JOKE"+PlayerPrefs.GetInt("No"), "one");
    17.        
    18.         PlayerPrefs.SetInt("No", no[1]);
    19.         PlayerPrefs.SetString("JOKE"+PlayerPrefs.GetInt("No"), "two");
    20.           }
    21. }
     
  4. Lukas H

    Lukas H

    Joined:
    Jan 16, 2009
    Posts:
    394
    Ok so this code is a bit different but this is what happends: you save int no[0] with key "No", two lines later you overwrite that value with int no[1].
    Its important to understand that if you want to save multiple values you need multiple and unique keys. (also if no[0] and no[1] are the same value, this example would fail too)
     
  5. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    so, do you know any other way to do this function?
     
  6. Lukas H

    Lukas H

    Joined:
    Jan 16, 2009
    Posts:
    394
    Start simple with something like:
    Code (csharp):
    1.  
    2. void Start()
    3. {
    4. PlayerPrefs.SetString("JOKE_ONE", "one");
    5. PlayerPrefs.SetString("JOKE_TWO", "two");
    6.  
    7. Debug.Log(PlayerPrefs.GetString("JOKE_ONE"));
    8. Debug.Log(PlayerPrefs.GetString("JOKE_TWO"));
    9. }
    10.  
    And build from there.
     
  7. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    but i want to save the more than 100 data in this.... so i need array function for this..
     
  8. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
    In this script i try to save the data when my toggle button is enable. if toggle is dis able that data should not save. This is my concept.

    Here is the script:
    Code (csharp):
    1. public string[] lables;
    2.     public bool[] selects;
    3.     public int[] no;
    4.     public GUIStyle lablestyle;
    5.    
    6.     public string[] saveditem;
    7.     static int save;
    8.     // Use this for initialization
    9.     void Start () {
    10.        
    11.         save = 1;
    12.         saveditem = new string[save];
    13.    
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.    
    19.     }
    20.     void OnGUI()
    21.     {  
    22.         for(int i =0; i < lables.Length-1; i++)
    23.         {
    24.             selects[i] = GUI.Toggle(new Rect(100, (30 * i) + 100, 20, 20), selects[i], "");
    25.         }
    26.                
    27.         if(GUI.Button(new Rect(130, 100, 100,30), "Press"))
    28.         {
    29.             save++;
    30.             saveditem = new string[save];
    31.         }
    32.         saveditem = new string[save];
    33.        
    34.         if(selects[0].Equals(true))
    35.         {
    36.             PlayerPrefs.SetString("Playre"+saveditem[save], lables[0].ToString());
    37.             print(lables[0].ToString());
    38.             save++;        
    39.         }
    40.        
    41.         if(selects[1].Equals(true))
    42.         {
    43.             PlayerPrefs.SetString("Playre"+saveditem[save], lables[0].ToString());
    44.             print(lables[0].ToString());
    45.             save++;        
    46.         }
    47.        
    48.         if(selects[2].Equals(true))
    49.         {
    50.             PlayerPrefs.SetString("Playre"+saveditem[save], lables[0].ToString());
    51.             print(lables[0].ToString());
    52.             save++;        
    53.         }
    54.        
    55.        
    56.        
    57.     }
     
  9. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
  10. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    424
  11. ardo314

    ardo314

    Joined:
    Jul 7, 2012
    Posts:
    345
    Why dont you use another for loop?