Home > Blockchain >  check 10 PlayerPrefs which is Empty and Save
check 10 PlayerPrefs which is Empty and Save

Time:02-11

i coded the following code i want to check which PlayerPrefs is empty and save text from inputbox then display it in Ui text

SaveTittlesText1.text=PlayerPrefs.GetString("1");
SaveTittlesText2.text=PlayerPrefs.GetString("2");
SaveTittlesText3.text=PlayerPrefs.GetString("3")

problem is Uitext3 is displaying text from Uitex2 please help i want to achieve this in more than 10 PlayerPrefs

if (PlayerPrefs.HasKey ("1") == false)
{
    PlayerPrefs.SetString("1",InputsaveTittes.text.ToString());

    PlayerPrefs.Save();
}
else if (PlayerPrefs.HasKey ("1") == true)
{
    PlayerPrefs.SetString("2",InputsaveTittes.text.ToString());

    PlayerPrefs.Save();
}
else if (PlayerPrefs.HasKey ("2") == false)
{
    PlayerPrefs.SetString("2",InputsaveTittes.text.ToString());

     PlayerPrefs.Save();
}
else if (PlayerPrefs.HasKey ("2") == true)
{
    PlayerPrefs.SetString("3",InputsaveTittes.text.ToString());

    PlayerPrefs.Save();
}

        

CodePudding user response:

I don't really understand that use case / logic but you are skipping a case:

if (PlayerPrefs.HasKey ("1") == false)
{
    // sets 1
}
else if (PlayerPrefs.HasKey("1") == true) // WHAT IF IT ALSO PlayerPrefs.HasKey ("2") == true here ?
{
    // sets 2
}
else if (PlayerPrefs.HasKey ("2") == false) // WILL NEVER BE REACHED
{
    // sets 2
}
else if (PlayerPrefs.HasKey ("2") == true) // WILL NEVER BE REACHED
{
    // sets 3
}

your last two cases will never be reached/checked at all since your first two cases already cover all possibilities!

you should either check like e.g.

if (!PlayerPrefs.HasKey ("1"))
{
    PlayerPrefs.SetString("1", InputsaveTittes.text);
}
// already know that 1 is set since otherwise it would have entered the first case
else if (!PlayerPrefs.HasKey ("2")) 
{
    PlayerPrefs.SetString("2", InputsaveTittes.text);
}
// Any other case -> this means 1 and 2 are already set
else 
{
    PlayerPrefs.SetString("3", InputsaveTittes.text);
}

PlayerPrefs.Save();

or in general use loops!

// iterate over 1,2,3
for(var i = 1; i <= 3; i  )
{
    // check if current index is already used
    if(!PlayerPrefs.HasKey(i))
    {
        // if not set it
        PlayerPrefs.SetString(i.ToString(), InputsaveTittes.text);
        PlayerPrefs.Save();

        // skip the rest of the loop
        break;
    }
}

Actually I would however rather simply have another entry for storing the current index - especially if you are going for more items - like e.g.

// defaults to 0 if there is no index yet -> first player will have index 1
var index = PlayerPrefs.GetInt("index", 0);
// increase index by 1
index  ;
// store string to this new index
PlayerPrefs.SetString(index.ToString(), InputsaveTittes.text);
// also store the new index
PlayerPrefs.SetInt("index", index);
PlayerPrefs.Save();

at the same time PlayerPrefs.GetInt("index", 0); will now also return the amount of already saved strings.


problem is Uitext3 is displaying text from Uitex2

This, however, sounds like you simply referenced the same Text component in both fields SaveTittlesText2 and SaveTittlesText3.

  • Related