• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

77 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

Related Questions

Problems with PlayerPrefs or something else 1 Answer

Issues creating proper level-unlocking script 0 Answers

Implementing currency with gameObjects 1 Answer

How do I save a custom class of variables to playerprefs? 2 Answers

How to load an inventory array? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges