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