- Home /
Level Selection and Strange Error
I followed this great tutorial video on level selection that doesn't let you move forward unless you have at least 1 out of 3 stars and it also keeps track of these stars. Sadly the code simply isn't working.
The error I keep getting is this
FormatException: Input string was not in a correct format.
System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at :0)
System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at :0)
System.Int32.Parse (System.String s) (at :0)
LevelSelection.UpdateLevelStatus () (at Assets/Scripts/LevelSelection.cs:23)
LevelSelection.Update () (at Assets/Scripts/LevelSelection.cs:18)
Here's the code, thank you to anyone who can help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LevelSelection : MonoBehaviour
{
[SerializeField] private bool unlocked;
public Image unlockImage;
public GameObject[] stars;
public Sprite starSprite;
private void Update()
{
UpdateLevelImage();
UpdateLevelStatus();
}
private void UpdateLevelStatus()
{
int previousLevelNum = int.Parse(gameObject.name) - 1;
if (PlayerPrefs.GetInt("lv" + previousLevelNum.ToString()) > 0)
{
unlocked = true;
}
}
private void UpdateLevelImage()
{
if(!unlocked)
{
unlockImage.gameObject.SetActive(true);
for(int i = 0; i < stars.Length; i++)
{
stars[i].gameObject.SetActive(false);
}
}
else
{
unlockImage.gameObject.SetActive(false);
for(int i = 0; i < stars.Length; i++)
{
stars[i].gameObject.SetActive(true);
}
}
}
public void PressSelection(string _LevelName)
{
if(unlocked)
{
SceneManager.LoadScene(_LevelName);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SingleLevel : MonoBehaviour
{
private int currentStarsNum = 0;
public int levelIndex;
public void BackButton()
{
SceneManager.LoadScene("LevelSelection");
}
public void PressStarsButton(int _starsNum)
{
currentStarsNum = _starsNum;
if (currentStarsNum > PlayerPrefs.GetInt("Lv" + levelIndex))
{
PlayerPrefs.SetInt("Lv" + levelIndex, _starsNum);
}
//BackButton();
//MARKER Each level has saved their own stars number
//CORE PLayerPrefs.getInt("KEY", "VALUE"); We can use the KEY to find Our VALUE
Debug.Log(PlayerPrefs.GetInt("Lv" + levelIndex, _starsNum));
BackButton();
}
}
The script Levelselection must be attached to a game object which name is a number, re-watch the tutorial and check where is he attaching the script and what's the name of that object