Hello,
Thanks for looking at this. As the title says I am working on creating a level select that is akin to the Angry Birds one. My problem is that it wont ever display anything past the first level. I wont it to have all the levels you havent played in red and locked so you cant play them. And then all the others to be green and playable. I’ll attach a photo at the end.
Oh and the script might be kind of messed up. I’ve been trying for the last couple of hours to get it to work and using all types of code I found on the forums and answers. All the code works and doesn’t give me errors but there might be stuff that’s not needed in it.
Here is the Level Select script:
using UnityEngine;
using System.Collections;
public class LevelSelect : MonoBehaviour {
public GUISkin skin;
public AudioSource[] sounds;
public AudioSource noise1;
public GameObject PlayerSaving;
private PlayerSaving playerSaving;
public Texture inActiveTexture;
public int level1 = 3;
public int level2 = 4;
public int level3 = 5;
public int level4 = 6;
public int level5 = 7;
void start()
{
playerSaving = GameObject.Find("PlayerSaving").GetComponent<PlayerSaving>();
sounds = GetComponents<AudioSource>();
noise1 = sounds[0];
}
void OnGUI()
{
GUI.skin = skin;
GUI.Label (new Rect(300,10,1000,120),"Levels");
if (GUI.Button(new Rect(10,10,275,100), "Main Menu"))
{
Application.LoadLevel(0);
noise1.Play ();
}
if (GUI.Button(new Rect(100,200,100,100), "1"))
{
Application.LoadLevel(3);
noise1.Play ();
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(215,200,100,100), "2"))
{
Application.LoadLevel(4);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(330,200,100,100), "3"))
{
Application.LoadLevel(5);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(445,200,100,100), "4"))
{
Application.LoadLevel(6);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(560,200,100,100), "5"))
{
Application.LoadLevel(7);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(675,200,100,100), "6"))
{
Application.LoadLevel(8);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(790,200,100,100), "7"))
{
Application.LoadLevel(9);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(905,200,100,100), "8"))
{
Application.LoadLevel(10);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(1020,200,100,100), "9"))
{
Application.LoadLevel(11);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(100,350,100,100), "10"))
{
Application.LoadLevel(12);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(215,350,100,100), "11"))
{
Application.LoadLevel(13);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(330,350,100,100), "12"))
{
Application.LoadLevel(14);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(445,350,100,100), "13"))
{
Application.LoadLevel(15);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(560,350,100,100), "14"))
{
Application.LoadLevel(16);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(675,350,100,100), "15"))
{
Application.LoadLevel(17);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(790,350,100,100), "16"))
{
Application.LoadLevel(18);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(905,350,100,100), "17"))
{
Application.LoadLevel(19);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(1020,350,100,100), "18"))
{
Application.LoadLevel(20);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(100,500,100,100), "19"))
{
Application.LoadLevel(21);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(215,500,100,100), "20"))
{
Application.LoadLevel(22);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(330,500,100,100), "21"))
{
Application.LoadLevel(23);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(445,500,100,100), "22"))
{
Application.LoadLevel(24);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(560,500,100,100), "23"))
{
Application.LoadLevel(25);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(675,500,100,100), "24"))
{
Application.LoadLevel(26);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(790,500,100,100), "25"))
{
Application.LoadLevel(27);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(905,500,100,100), "26"))
{
Application.LoadLevel(28);
noise1.Play ();
}
}
if(playerSaving.maxPlayedLevel > playerSaving.levelNumber)
{
if (GUI.Button(new Rect(1020,500,100,100), "27"))
{
Application.LoadLevel(29);
noise1.Play ();
}
}
}
}
I’m really sorry for how cluttered it is. I’m just beginning to learn coding and I’m not very good at refining it.
Here is the second script which saves what the last level the player was on which is used in the other one to display the level.
using UnityEngine;
using System.Collections;
public class PlayerSaving : MonoBehaviour {
public int currentLevel = 3;
public int lockedLevels;
public int maxPlayedLevel = 3;
public int levelNumber;
//Sound Vars
public AudioSource[] sounds;
public AudioSource noise1;
//Scoring Variables
public int currentScore;
public int highscore;
public int tokenCount;
private int totalTokenCount;
//All winScreen stuff.
public GUISkin skin;
public GameObject tokenParent;
public Rect timerRect;
public Color warningColorTimer;
public Color defaultColorTimer;
public float startTime;
private string currentTime;
public Color WinScreen;
private bool completed = false;
private bool showWinScreen = false;
public int winScreenWidth, winScreenHeight;
// Use this for initialization
void Start () {
totalTokenCount = tokenParent.transform.childCount;
levelNumber = currentLevel;
if (PlayerPrefs.GetInt("levelNumber") > maxPlayedLevel)
{
maxPlayedLevel = PlayerPrefs.GetInt ("levelNumber");
sounds = GetComponents<AudioSource>();
noise1 = sounds[0];
} else {
maxPlayedLevel = 3;
}
}
void Update()
{
if (!completed)
{
startTime -= Time.deltaTime;
currentTime = string.Format("{0:0.0}", startTime);
if (startTime <= 0)
{
startTime = 0;
Application.LoadLevel(0);
}
}
}
void LoadNextLevel()
{
if (currentLevel == maxPlayedLevel)
{
currentLevel += 1;
maxPlayedLevel += 1;
SaveGame();
Application.LoadLevel(currentLevel);
}
else
{
currentLevel += 1;
maxPlayedLevel += 1;
Application.LoadLevel (currentLevel);
}
}
void SaveGame()
{
PlayerPrefs.SetInt("Level " + currentLevel.ToString () + " score", currentScore);
PlayerPrefs.SetInt ("maxPlayedLevel", currentLevel);
PlayerPrefs.SetInt("currentLevel", levelNumber);
PlayerPrefs.Save();
}
public void CompleteLevel()
{
showWinScreen = true;
completed = true;
}
void OnGUI()
{
GUI.skin = skin;
if (startTime < 10f)
{
skin.GetStyle("Timer").normal.textColor = warningColorTimer;
} else {
skin.GetStyle("Timer").normal.textColor = defaultColorTimer;
}
GUI.Label (timerRect, currentTime, skin.GetStyle ("Timer"));
GUI.Label (new Rect(45,100,200,200),tokenCount.ToString() + "/" + totalTokenCount.ToString());
if (showWinScreen)
{
Rect winScreenRect = new Rect(Screen.width/2 - (Screen.width *.5f/2), Screen.height/2 -(Screen.height *.5f/2), Screen.width *.5f, Screen.height *.5f);
GUI.Box(winScreenRect,"Yeah");
int gameTime = (int)startTime;
currentScore = tokenCount * gameTime * 2;
if(GUI.Button(new Rect(winScreenRect.x + winScreenRect.width - 260, winScreenRect.y + winScreenRect.height - 50, 250, 40), "Continue"))
{
LoadNextLevel();
}
if (GUI.Button(new Rect(winScreenRect.x + winScreenRect.width - 630, winScreenRect.y + winScreenRect.height - 50, 100, 40), "Quit"))
{
Application.LoadLevel(0);
}
GUI.Label (new Rect(winScreenRect.x + 20, winScreenRect.y + 40, 300, 50), currentScore.ToString() + " Score");
GUI.Label (new Rect(winScreenRect.x + 20, winScreenRect.y + 90, 300, 50), "Completed Level " + currentLevel);
}
if (GUI.Button(new Rect(1170,10,100,100), "Quit"))
{
Application.LoadLevel(0);
noise1.Play ();
}
if (GUI.Button(new Rect(1170,110,100,100), "R"))
{
Application.LoadLevel(currentLevel);
noise1.Play ();
}
}
}
Again sorry for the cluster.
After playing around more I think that the error has something to do with the Level Select script not getting the other scripts stuff. I get and error saying something like “The object reference is not set to an instance of an object.” Something like that.
Heres the pic. Notice only level 1 shows up and none of the others.
Thanks in advance for all your help.