- Home /
Array not displaying my level best time in my level select menu
I'm trying to display a text on my level select screen that shows my best time. I can check my registry and it shows the best time in there, but it will not show it on my text object or in my level select menu. It only displays 0:00 on under all of my level select thumbnails.
my Instantiated prefab looks like this: Canvas>LevelButtonPrefab>BottomPanel(with the text for best time)
Help would be greatly appreciated as I cannot find my error. I need some fresh eyes!
Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class LevelData {
public LevelData (string levelName)
{
string data = PlayerPrefs.GetString (levelName);
if (data == "")
{
return;
}
string[] allData = data.Split ('&');
BestTime = float.Parse (allData [0]);
SilverTime = float.Parse (allData [1]);
GoldTime = float.Parse (allData [2]);
}
public float BestTime { set; get; }
public float SilverTime { set; get; }
public float GoldTime { set; get; }
}
public class MainMenu : 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 Transform cameraTransform;
private Transform cameraDesiredLookAt;
//private bool nextLevelLocked = false;
private int[] costs = {0,150,150,150,
300,300,300,300,
500,500,500,500,
1000,1250,1500,2000};
private void Start()
{
ChangePlayerSkin(GameManager.Instance.currentSkinIndex);
currencyText.text = "Currency : " + GameManager.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.BestTime != 0.0f) ? level.BestTime.ToString ("f") : "";
//container.transform.GetChild(1).GetComponent<Image>().enabled = nextLevelLocked;
//container.GetComponent<Button>().interactable = !nextLevelLocked;
/*if(level.BestTime == 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)
{
//Sprite texture = textures[i];
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 ();
if ((GameManager.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,
CAMERA_TRANSITION_SPEED * Time.deltaTime);
}
}
private void LoadLevel (string sceneName)
{
SceneManager.LoadScene (sceneName);
}
public void LookAtMenu(Transform menuTransform)
{
cameraDesiredLookAt = menuTransform;
}
private void ChangePlayerSkin(int index)
{
if ((GameManager.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));
GameManager.Instance.currentSkinIndex = index;
GameManager.Instance.Save();
}
else
{
//you do not have the skin, do you want to buy it?
int cost = costs[index];
if(GameManager.Instance.currency >= cost)
{
GameManager.Instance.currency -= cost;
GameManager.Instance.skinAvailability += 1 << index;
GameManager.Instance.Save();
currencyText.text = "Currency : " + GameManager.Instance.currency.ToString();
if (shopButtonContainer.transform.childCount > 0)
{
shopButtonContainer.transform.GetChild(index).GetChild(0).gameObject.SetActive(false);
}
ChangePlayerSkin(index);
}
}
}
},I'm working on a game that will display the best time for each level underneath the thumbnail for the level. The Level Container is a prefab that instantiates at runtime. It looks like this Canvas LevelButtonPrefab Panel text(for best time)
My issue is that although it will record my best time in my registry, it does not display it in the text field in run time on the level selection screen, or in the text object itself. It will only display 0:00.
Its driving me crazy!!
Here is my code.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class LevelData {
public LevelData (string levelName)
{
string data = PlayerPrefs.GetString (levelName);
if (data == "")
{
return;
}
string[] allData = data.Split ('&');
BestTime = float.Parse (allData [0]);
SilverTime = float.Parse (allData [1]);
GoldTime = float.Parse (allData [2]);
}
public float BestTime { set; get; }
public float SilverTime { set; get; }
public float GoldTime { set; get; }
}
public class MainMenu : 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 Transform cameraTransform;
private Transform cameraDesiredLookAt;
//private bool nextLevelLocked = false;
private int[] costs = {0,150,150,150,
300,300,300,300,
500,500,500,500,
1000,1250,1500,2000};
private void Start()
{
ChangePlayerSkin(GameManager.Instance.currentSkinIndex);
currencyText.text = "Currency : " + GameManager.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.BestTime != 0.0f) ? level.BestTime.ToString ("f") : "";
//container.transform.GetChild(1).GetComponent<Image>().enabled = nextLevelLocked;
//container.GetComponent<Button>().interactable = !nextLevelLocked;
/*if(level.BestTime == 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)
{
//Sprite texture = textures[i];
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 ();
if ((GameManager.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,
CAMERA_TRANSITION_SPEED * Time.deltaTime);
}
}
private void LoadLevel (string sceneName)
{
SceneManager.LoadScene (sceneName);
}
public void LookAtMenu(Transform menuTransform)
{
cameraDesiredLookAt = menuTransform;
}
private void ChangePlayerSkin(int index)
{
if ((GameManager.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));
GameManager.Instance.currentSkinIndex = index;
GameManager.Instance.Save();
}
else
{
//you do not have the skin, do you want to buy it?
int cost = costs[index];
if(GameManager.Instance.currency >= cost)
{
GameManager.Instance.currency -= cost;
GameManager.Instance.skinAvailability += 1 << index;
GameManager.Instance.Save();
currencyText.text = "Currency : " + GameManager.Instance.currency.ToString();
if (shopButtonContainer.transform.childCount > 0)
{
shopButtonContainer.transform.GetChild(index).GetChild(0).gameObject.SetActive(false);
}
ChangePlayerSkin(index);
}
}
}
}
Thanks for any help!!!!
Your answer
Follow this Question
Related Questions
Most efficient way to convert string[] to int [] in C#? 2 Answers
Convert Text to float 3 Answers
Monodevelop is expecting int from my float array 3 Answers
Multiple Cars not working 1 Answer