Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
check 10 PlayerPrefs which is Empty and Save

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();
}

        
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!