I am building a game consisting of 8 levels. I use the following script to switch next level. But when I unlock a level the Lock Texture still remain in the window. I want to remove the lock texture when i unlock a new level. Please help me out.
My code is below:
LevelIndex = 0;
for ( GridIndexB = 0 ; GridIndexB < LevelsGrid.y ; GridIndexB++ )
{
for ( GridIndexA = 0 ; GridIndexA < LevelsGrid.x ; GridIndexA++ )
{
LevelIndex++;
GUI.DrawTexture(Rect( (Screen.width - LevelsGrid.x * (LevelTexture.width + 10)) * 0.5 + GridIndexA * (LevelTexture.width + 10), 60 + GridIndexB * (LevelTexture.height + 10) ,LevelTexture.width, LevelTexture.height), LevelTexture);//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
if ( TotalPoints >= PointsToUnlock[LevelIndex - 1] )
{
if ( GUI.Button(Rect( (Screen.width - LevelsGrid.x * (LevelTexture.width + 10)) * 0.5 + GridIndexA * (LevelTexture.width + 10), 60 + GridIndexB * (LevelTexture.height + 10), LevelTexture.width, LevelTexture.height), LevelIndex.ToString()) )//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
{
PlayerPrefs.SetInt("currentLevel", LevelIndex);
Application.LoadLevel("level" + LevelIndex);
}
}
else
{
GUI.DrawTexture(Rect( (Screen.width - LevelsGrid.x * (LevelTexture.width + 10)) * 0.5 + GridIndexA * (LevelTexture.width + 10), 60 + GridIndexB * (LevelTexture.height + 10), LevelTexture.width, LevelTexture.height), LockTexture);
}
}
}
The full script looks like this:
var TitleText:String = "SINGLE PLAYER";
var NumberOfPlayers:int = 1;
var Levels:int = 8;
private var LevelIndex:int = 0;
var PointsToUnlock:Array = new Array(0, 2, 6, 12, 24, 36, 48, 56);
var LevelsGrid:Vector2 = Vector2(4,2);
private var GridIndexA:int = 0;
private var GridIndexB:int = 0;
var LevelTexture:Texture2D;
var LockTexture:Texture2D;
//~ var HowToPlayTexture:Texture2D;
var BGPattern:Texture2D;
//This script displays the HUD, with current weapon, ammo left, Health, Shield, and score
var GUIskin:GUISkin; //The skin gui we'll use
private var LevelPoints:Array = new Array();
private var TotalPoints:int = 0;
function Start()
{
if ( camera.main.GetComponent("StartMenu") )
camera.main.GetComponent("StartMenu").MenuLevel = transform;
else if ( camera.main.GetComponent("EndMenu") )
camera.main.GetComponent("EndMenu").MenuLevel = transform;
LevelPoints.Clear();
for ( LevelIndex = 1 ; LevelIndex <= transform.GetComponent("LevelsMenu").Levels ; LevelIndex++ )
{
LevelPoints.Push(PlayerPrefs.GetInt(("level" + LevelIndex + "Points").ToString()));
TotalPoints += LevelPoints[LevelIndex - 1];
}
print(LevelPoints);
print(TotalPoints);
}
function OnGUI()
{
GUI.skin = GUIskin; //The skin gui we'll use
GUI.depth = 0;
GUI.DrawTexture(Rect( 0, 0 ,Screen.width, Screen.height), BGPattern);//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
GUI.Label(Rect(0, 10, Screen.width, 70), TitleText);
LevelIndex = 0;
for ( GridIndexB = 0 ; GridIndexB < LevelsGrid.y ; GridIndexB++ )
{
for ( GridIndexA = 0 ; GridIndexA < LevelsGrid.x ; GridIndexA++ )
{
LevelIndex++;
GUI.DrawTexture(Rect( (Screen.width - LevelsGrid.x * (LevelTexture.width + 10)) * 0.5 + GridIndexA * (LevelTexture.width + 10), 60 + GridIndexB * (LevelTexture.height + 10) ,LevelTexture.width, LevelTexture.height), LevelTexture);//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
if ( TotalPoints >= PointsToUnlock[LevelIndex - 1] )
{
if ( GUI.Button(Rect( (Screen.width - LevelsGrid.x * (LevelTexture.width + 10)) * 0.5 + GridIndexA * (LevelTexture.width + 10), 60 + GridIndexB * (LevelTexture.height + 10), LevelTexture.width, LevelTexture.height), LevelIndex.ToString()) )//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
{
PlayerPrefs.SetInt("currentLevel", LevelIndex);
Application.LoadLevel("level" + LevelIndex);
}
}
else
{
GUI.DrawTexture(Rect( (Screen.width - LevelsGrid.x * (LevelTexture.width + 10)) * 0.5 + GridIndexA * (LevelTexture.width + 10), 60 + GridIndexB * (LevelTexture.height + 10), LevelTexture.width, LevelTexture.height), LockTexture);
}
}
}
if ( GUI.Button(Rect( 10, Screen.height - 70, 140, 60), "BACK") )//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
{
if ( camera.main.GetComponent("StartMenu") )
camera.main.GetComponent("StartMenu").MenuLevel = camera.main.transform;
else if ( camera.main.GetComponent("EndMenu") )
camera.main.GetComponent("EndMenu").MenuLevel = camera.main.transform;
Destroy(gameObject);
}
if ( GUI.Button(Rect( Screen.width - 410 , Screen.height - 70, 400, 60), "RESET LEVELS") )//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
{
LevelPoints.Clear();
for ( LevelIndex = 1 ; LevelIndex <= transform.GetComponent("LevelsMenu").Levels ; LevelIndex++ )
{
PlayerPrefs.SetInt(("level" + LevelIndex + "Points").ToString(), 0);
}
TotalPoints = 0;
}
}