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 /
  • Help Room /
avatar image
0
Question by piero-trujillo · Aug 11, 2016 at 11:42 PM · unity 5unity 2dshopbuycurrency

Player Skins Won't Load

I'm making a character shop where the player can buy different players with currency.

However, the currency won't show and the skins won't spawn.

I'm following this tutorial.

My code:

SkinMenu.

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 using System.Collections;
 
 public class LevelData 
 {
     public LevelData (string levelName)
     {
         string data = PlayerPrefs.GetString(levelName);
         if(data == "")
             return;
 
         string[] allData = data.Split ('&');
         bestScore = float.Parse (allData[0]);
         SilverScore = float.Parse (allData[1]);
         GoldScore = float.Parse (allData[2]);
     }
     public float bestScore { set; get; }
     public float GoldScore { set; get; }
     public float SilverScore { set; get; }
 }
 
 public class SkinMenu : MonoBehaviour {
 
     private const float CAMERA_TRANSITION_SPEED = 3.0f;
 
     public GameObject levelButtonPrefab;
     public GameObject levelButtonContainer;
     public GameObject shopButtonPrefab;
     public GameObject shopButtonContainer;
 
     public Text currencyText;
 
     public Material playerMaterial;
 
     private bool nextLevelLocked;
 
     private Transform cameraTransform;
     private Transform cameraDesiredLookAt;
 
     private int[] costs = {0, 150, 300};
 
 
 
     // Use this for initialization
     private void Start () {
         //PlayerPrefs.DeleteAll ();
         ChangePlayerSkin (SkinManager.Instance.currentSkinIndex);
         currencyText.text = "Currency : " + SkinManager.Instance.currency.ToString ();
         cameraTransform = Camera.main.transform;
 
         Sprite[] thumbnails = Resources.LoadAll<Sprite> ("Levels");
 
         foreach (Sprite thumbnail in thumbnails)
         {
             GameObject container = Instantiate (levelButtonPrefab) as GameObject;
             container.GetComponent<Image> ().sprite = thumbnail;
             container.transform.SetParent (levelButtonContainer.transform, false);
             LevelData level = new LevelData (thumbnail.name);
             container.transform.GetChild (0).GetChild(0).GetComponent<Text>().text = (level.bestScore != 0.0f) ? level.bestScore.ToString("F") : "LOCKED";
 
             container.transform.GetChild (1).GetComponent<Image>().enabled = nextLevelLocked;
 
 
             container.GetComponent<Button> ().interactable = !nextLevelLocked;
             if(level.bestScore == 0.0f)
             {
                 nextLevelLocked = true;
             }
             string sceneName = thumbnail.name;
             container.GetComponent<Button> ().onClick.AddListener (() => LoadLevel (sceneName));
         }    
         
                 int textureIndex = 0;
                 Sprite[] textures = Resources.LoadAll<Sprite> ("Player");
                 foreach (Sprite texture in textures)
                 {
                     GameObject container = Instantiate (shopButtonPrefab) as GameObject;
                     container.GetComponent<Image> ().sprite = texture;
                     container.transform.SetParent (shopButtonContainer.transform, false);
 
                     int index = textureIndex;
                     container.GetComponent<Button>().onClick.AddListener (() => ChangePlayerSkin(index));
             container.transform.GetChild (0).GetChild(0).GetComponent<Text>().text = costs [index].ToString();
             //container.transform.GetComponentInChildren<tExt>()
                     if((SkinManager.Instance.skinAvailability & 1 << index) == 1 << index)
                     {
                     container.transform.GetChild (0).gameObject.SetActive (false);
                     }
                     textureIndex++;
                 }
     }
 
     private void Update () 
     {
                     if(cameraDesiredLookAt !=null)
                     {
             cameraTransform.rotation = Quaternion.Slerp (cameraTransform.rotation, cameraDesiredLookAt.rotation, 3 * Time.deltaTime);
                     }
     }
     private void LoadLevel (string sceneName)
                 {
                     SceneManager.LoadScene (sceneName);
                 }
     private void LookAtMenu(Transform menuTransform)
                 {
                     cameraDesiredLookAt = menuTransform;
                 }
     private void ChangePlayerSkin(int index)
     {
         if ((SkinManager.Instance.skinAvailability & 1 << index) == 1 << index) {
 
             float x = (index % 4) * 0.25f;
             float y = ((int)index / 4) * 0.25f;
 
             if (y == 0.0f)
                 y = 0.75f;
             else if (y == 0.25f)
                 y = 0.5f;
             else if (y == 0.50f)
                 y = 0.25f;
             else if (y == 0.75f)
                 y = 0f;
 
             playerMaterial.SetTextureOffset ("_MainTex", new Vector2 (x, y));
 
             SkinManager.Instance.currentSkinIndex = index;
             SkinManager.Instance.Save ();
         } else {
             //don't have the skin wanna buy it?
             int cost = costs [index];
             //Debug.Log (cost);
             if (SkinManager.Instance.currency >= cost) {
                 SkinManager.Instance.currency -= cost;
                 SkinManager.Instance.skinAvailability += 1 << index;
                 SkinManager.Instance.Save ();
                 currencyText.text = "Currency : " + SkinManager.Instance.currency.ToString ();
                 shopButtonContainer.transform.GetChild (index).GetChild (0).gameObject.SetActive (false);
                 ChangePlayerSkin (index);
             }
         }
                         
     }
 
 
 
 
 
 }    

SkinManager.

 using UnityEngine;
 using System.Collections;
 
 public class SkinManager : MonoBehaviour {
 
     private static SkinManager instance;
     public static SkinManager Instance { get { return instance; } }
 
     public int currentSkinIndex = 0;
     public int currency = 0;
     public int skinAvailability = 1;
 
     private void Awake()
     {
         instance = this;
         DontDestroyOnLoad (gameObject);
 
         if (PlayerPrefs.HasKey ("CurrentSkin")) {
             // We had a previous session
             currentSkinIndex = PlayerPrefs.GetInt("CurrentSkin");
             currency = PlayerPrefs.GetInt("Currency");
             skinAvailability = PlayerPrefs.GetInt("SkinAvailability");
         } else {
             Save ();
         }
     }
 
     public void Save ()
     {
         PlayerPrefs.SetInt ("CurrentSkin", currentSkinIndex);
         PlayerPrefs.SetInt ("Currency", currency);
         PlayerPrefs.SetInt ("SkinAvailability", skinAvailability);
     }
 }



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

0 Replies

· Add your reply
  • Sort: 

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

98 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

Related Questions

How to Setactive(true) the other class ? 0 Answers

Prefab Direction - shooting 2 Answers

How to make script remember a counter and display an ad every 5th time? 1 Answer

When I used portal in My Game why Destroyed “Camera Fallow”? 0 Answers

How to make a follow camera code and jump sistem 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