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 /
avatar image
0
Question by mihairey · Nov 06, 2017 at 09:02 PM · playerprefsarraysshop

About playerprefs, character selector and shop

Hello guys, I want to build a shop and i have created a character selection mode until now. So basicly i have a script that looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class CharacterSelection : MonoBehaviour {
 
     private GameObject[] characterList;
     private int index;
     public string lvltoload = "MainLevel";
 
     private void Start()
     {
         index = PlayerPrefs.GetInt("CharacterSelected");
         characterList = new GameObject[transform.childCount];
 
         for (int i = 0; i < transform.childCount; i++)
         {
             characterList[i] = transform.GetChild(i).gameObject;
         }
         foreach (GameObject go in characterList)
         {
             go.SetActive(false);
         }
         if (characterList[index])
         {
             characterList[index].SetActive(true);
         }
     }
 
     public void ToggleLeft()
     {
         characterList[index].SetActive(false);
 
         index -= 1;
         if(index < 0)
         {
             index = characterList.Length - 1;
         }
 
         characterList[index].SetActive(true);
     }
 
     public void ToggleRight()
     {
         characterList[index].SetActive(false);
 
         index += 1;
         if (index == characterList.Length)
         {
             index = 0;
         }
 
         characterList[index].SetActive(true);
     }
     public void ConfirmButton()
     {
         PlayerPrefs.SetInt("CharacterSelected", index);
         SceneManager.LoadScene(lvltoload);
     }
 }

And i want the user to firstly buy the character and after that to select it. But i don't know how to do it. I was thinking to set a playerprefs but i don't know how to set it (the script) for each index. For example if index 1(character 1) has the PlayerPrefs ("CharacterSold", 0) then the button select to be inactive. And now the player should buy the character. So the PlayerPrefs becomes 1 and now the select button is active. Can you help me with that?

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 FlaSh-G · Nov 07, 2017 at 10:45 AM

So basically your question is "how do I save an arbitrary amount of values into PlayerPrefs"?

There's multiple ways to do that. Especially since your values are simple booleans. One could be a string with lots of ones and zeroes:

 // Load
 var characterUnlocks = PlayerPrefs.GetString("CharacterUnlocks");
 for(var i = 0; i < characterUnlocks.Length; i++)
 {
   // Doesn't work, but it shows the idea
   characterList[i].unlocked = characterUnlocks[i] == '1';
 }
 
 // Save
 var sb = new StringBuilder();
 for(var i = 0; i < characterList.Length; i++)
 {
   sb.Append(characterList[i].unlocked ? '1' : '0');
 }
 PlayerPrefs.SetString("CharacterUnlocks", sb.ToString());

Of course, there is no such thing as GameObject.unlocked, you'll have to write something there. Probably reference some sort of character script in your array rather than the GameObjects.

Another way would be to store multiple PlayerPrefs data points:

 // Load
 for(var i = 0; i < characterList.Length; i++)
 {
   characterList[i].unlocked = PlayerPrefs.GetInt("UnlockedCharacter" + i, 0) == 1;
 }
 
 // Save
 for(var i = 0; i < characterList.Length; i++)
 {
   PlayerPrefs.SetInt("UnlockedCharacter" + i, characterList[i].unlocked ? 1 : 0);
 }

Note the PlayerPrefs key string based on the value of i.

Comment
Add comment · Show 1 · 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 mihairey · Nov 07, 2017 at 12:18 PM 0
Share

Thank you very much sir. I will try it.

avatar image
0

Answer by madhu_kumar · Nov 07, 2017 at 12:50 PM

for(var i = 0; i < characterList.Length; i++) { if(PlayerPrefs.GetInt("UnlockedCharacter" + i) == 0) // { //not buyed } if(PlayerPrefs.GetInt("UnlockedCharacter" + i) == 1) // { //buyed but not selected } if(PlayerPrefs.GetInt("UnlockedCharacter" + i) == 2) // { //selected } }

Comment
Add comment · Show 1 · 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 madhu_kumar · Nov 07, 2017 at 12:51 PM 0
Share

for(var i = 0; i < characterList.Length; i++) { if(PlayerPrefs.GetInt("UnlockedCharacter" + i) == 0) { //not buyed } if(PlayerPrefs.GetInt("UnlockedCharacter" + i) == 1) { //buyed but not selected } if(PlayerPrefs.GetInt("UnlockedCharacter" + i) == 2) { //selected } }

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

75 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

Related Questions

How do to you save time for each player and display it? 2 Answers

How do I save and load a 2D array? 1 Answer

save string from array to player prefs 1 Answer

Can anyone help me figure out why PlayerPrefs wont save the money value?,Can anyone help me as to why my money won't save in my game? Using PlayerPrefs 1 Answer

Using PlayerPrefs to save a C# Generic List 1 Answer


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