Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 L9Zaro · Aug 04, 2020 at 11:52 AM · scripting problemuiplayerprefsshopcharacter customization

Unity Character Selection + store

Hi! I've been having trouble making a character selection screen for my game. My goal is to make a character selection scene where you could purchase characters with coins that you collect from the game. I created the scripts from what I've learned from these two videos: Character Select Scene and Simple Shop.

I combined the two videos, what I came up with is a character select screen similar to the first video with a buy buttonalt text

All the character models are attached to an empty game object called player models with this script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class CharacterScript : MonoBehaviour
 {
     private GameObject[] characterList;
     private int index;
     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--;
         if (index < 0)
             index = characterList.Length - 1;
         characterList[index].SetActive(true);
     }
 
     public void ToggleRight()
     {
        
             characterList[index].SetActive(false);
             index++;
             if (index == characterList.Length)
                 index = 0;
             characterList[index].SetActive(true);
         
     }
 
     public void ConfirmButton()
     {
         PlayerPrefs.SetInt("CharacterSelected", index);
         SceneManager.LoadScene(1);
     }
 
     public void MainMenuButton()
     {
         
         SceneManager.LoadScene(0);
     }
 }


It basically controls the arrow buttons and the confirm button which works perfectly. The issue is the buying part.

I currently have 37 models. What I've done is create duplicates for the buy button and the confirm button and placed them as a child for each character model (So basically there are 37 buy buttons and confirm buttons) and I also have multiple copies of this script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class f_char02 : MonoBehaviour
 {
     int coinAmount1;
     int isBought1;
     public Text coinAmountText1;
     public Button confirmButton1;
     public Button buyButton1;
 
     void Start()
     {
         coinAmount1 = PlayerPrefs.GetInt("Coins");
         confirmButton1.gameObject.SetActive(true);
         buyButton1.gameObject.SetActive(true);
     }
     private void Update()
     {
 
         coinAmountText1.text = "x " + coinAmount1.ToString();
         isBought1 = PlayerPrefs.GetInt("IsBought");
 
         if (coinAmount1 >= 50 && isBought1 == 0)
         {
             buyButton1.interactable = true;
             confirmButton1.interactable = false;
         }
         else if (coinAmount1 >= 50 && isBought1 == 1)
         {
             buyButton1.interactable = false;
             confirmButton1.interactable = true;
         }
         else if (coinAmount1 <= 50 && isBought1 == 0)
         {
             confirmButton1.interactable = false;
             buyButton1.interactable = false;
         }
         else if (coinAmount1 <= 50 && isBought1 == 1)
         {
             confirmButton1.interactable = true;
             buyButton1.interactable = false;
         }
 
     }
 
     public void BuyButton()
     {
         coinAmount1 -= 50;
         PlayerPrefs.SetInt("IsBought", 1);
         buyButton1.gameObject.SetActive(false);
 
 
     }
 }
 
 

which is in each of the character models. When I press buy on one model, it purchases every single other character in the game even though I only bought the first one which does not make sense given the fact that I duplicated every single button there is. Another problem is that my gold is reduced when hovering over the character I bought but when I switch to a different character my gold is back where it started. I am open to remaking this character select menu and I will accept all suggestions

Sorry for the long post I just wanted to make this as detailed as possible, thanks in advance

character-select.jpg (397.7 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by davidcox70 · Aug 04, 2020 at 02:17 PM

This is because you only have a single saved user setting called "IsBought". You said that this same code is on all your purchase options, so which ever one is purchased will set IsBought=1 and save it as a user preference. This single setting is then referenced by every single model, which is what is making them appear as purchased. So you will need to have a different users prefs variable for each model.

Comment
Add comment · Show 4 · 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 L9Zaro · Aug 05, 2020 at 02:23 PM 0
Share

Yeah, I remade the code yesterday, I gave the variables different names like: isbought1 isbought2 and so on but some issues still persist like my money resetting back to what it used to be before I bought the item when I switch scenes. But Giving them different names solved one part of the problem though. Thanks

avatar image davidcox70 L9Zaro · Aug 05, 2020 at 02:40 PM 0
Share

Good to know. Please accept the answer to round off the question. Regarding money resetting, a good practice when using playerPrefs is to enforce saving by adding a PlayerPrefs.Save(); when you've set some variables. This will make sure the data is saved to the devices storage as otherwise it gets saved at a later time, which might not be in time for your next reading of that data.

avatar image L9Zaro davidcox70 · Aug 07, 2020 at 06:57 AM 0
Share

Hey I found a new issue. What happened is I collected 500 coins then I spent it on one character and my coins are reduced by 50 which is what is supposed to happen, then I switch to another model to attempt to buy it but my coins went back up to 500

Show more comments

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

310 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 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 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 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 avatar image avatar image

Related Questions

shop script not working 1 Answer

How to make Input.GetAxis work on certain area only? 0 Answers

Problems in the script for making a text disappear after few seconds 2 Answers

Pull a random input field from one scene into another 1 Answer

Problem with UI Text displaying a score 0 Answers


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