Hello, so to finish off my game (iPhone Game) I added a lock/unlocking system for levels using the PlayerPrefs. Currently when I play the game it crashes when I touch an object that I can pick up, probably because it is trying to update the score, and also this only happens when the playerprefs are in my script. My script is also not updating the playerprefs for some reason.
Does anyone know why this is happening?
Mainscript:
var LE1 = PlayerPrefsX.GetBool("LevelOneEnabled");
var LE2 = PlayerPrefsX.GetBool("LevelTwoEnabled");
var LE3 = PlayerPrefsX.GetBool("LevelThreeEnabled");
var LE4 = PlayerPrefsX.GetBool("LevelFourEnabled");
var LE5 = PlayerPrefsX.GetBool("LevelFiveEnabled");
var LE6 = PlayerPrefsX.GetBool("LevelSixEnabled");
var LE7 = PlayerPrefsX.GetBool("LevelSevenEnabled");
var LE8 = PlayerPrefsX.GetBool("LevelEightEnabled");
var LE9 = PlayerPrefsX.GetBool("LevelNineEnabled");
var LE10 = PlayerPrefsX.GetBool("LevelTenEnabled");
var GUIicon1 : Texture2D;
var GUIicon2 : Texture2D;
var GUIicon3 : Texture2D;
var GUIicon4 : Texture2D;
var GUIicon5 : Texture2D;
var GUIicon6 : Texture2D;
var GUIicon7 : Texture2D;
var GUIicon8 : Texture2D;
var GUIicon9 : Texture2D;
var GUIicon10 : Texture2D;
function OnGUI () {
GUI.enabled = LE1;
if (GUI.Button(Rect(120,100,40,40), (GUIicon1)))
{
Application.LoadLevel(11);
}
GUI.enabled = LE2;
if (GUI.Button(Rect(170,100,40,40), (GUIicon2)))
{
Application.LoadLevel(12);
}
GUI.enabled = LE3;
if (GUI.Button(Rect(220,100,40,40), (GUIicon3)))
{
Application.LoadLevel(13);
}
GUI.enabled = LE4;
if (GUI.Button(Rect(270,100,40,40), (GUIicon4)))
{
Application.LoadLevel(14);
}
GUI.enabled = LE5;
if (GUI.Button(Rect(320,100,40,40), (GUIicon5)))
{
Application.LoadLevel(15);
}
GUI.enabled = LE6;
if (GUI.Button(Rect(120,175,40,40), (GUIicon6)))
{
Application.LoadLevel(16);
}
GUI.enabled = LE7;
if (GUI.Button(Rect(170,175,40,40), (GUIicon7)))
{
Application.LoadLevel(17);
}
GUI.enabled = LE8;
if (GUI.Button(Rect(220,175,40,40), (GUIicon8)))
{
Application.LoadLevel(18);
}
GUI.enabled = LE9;
if (GUI.Button(Rect(270,175,40,40), (GUIicon9)))
{
Application.LoadLevel(19);
}
GUI.enabled = LE10;
if (GUI.Button(Rect(320,175,40,40), (GUIicon10)))
{
Application.LoadLevel(20);
}
GUI.enabled = true;
}
Updating Playerprefs Script:
private function EndGame()
{
var animationController : AnimationController = GetComponent( AnimationController );
var prefab : GameObject = Instantiate(guiMessage);
var endMessage : GUIText = prefab.GetComponent( GUIText );
if(carrying >= winScore)
{
//Player wins
endMessage.text = "You win!";
PlayAudioClip( winSound, Vector3.zero, 1.0 );
// animationController.animationTarget.Play( "WIN" );
if (level == 1){
PlayerPrefsX.SetBool("LevelTwoEnabled", true);
}
if (level == 2){
PlayerPrefsX.SetBool("LevelThreeEnabled", true);
}
if (level == 3){
PlayerPrefsX.SetBool("LevelFourEnabled", true);
}
if (level == 4){
PlayerPrefsX.SetBool("LevelFiveEnabled", true);
}
if (level == 5){
PlayerPrefsX.SetBool("LevelSixEnabled", true);
}
if (level == 6){
PlayerPrefsX.SetBool("LevelSevenEnabled", true);
}
if (level == 7){
PlayerPrefsX.SetBool("LevelEightEnabled", true);
}
if (level == 8){
PlayerPrefsX.SetBool("LevelNineEnabled", true);
}
if (level == 9){
PlayerPrefsX.SetBool("LevelTenEnabled", true);
}
}
else
{
//Player loses
endMessage.text = "You Lose";
PlayAudioClip( loseSound, Vector3.zero, 1.0 );
// animationController.animationTarget.Play( "LOSE" );
}
// Alert other components on this GameObject that the game has ended
SendMessage( "OnEndGame" );
while( true )
{
// Wait for a touch before reloading the intro level
yield WaitForFixedUpdate();
if ( iPhoneInput.touchCount > 0 && iPhoneInput.GetTouch( 0 ).phase == iPhoneTouchPhase.Began )
break;
}
Application.LoadLevel( 22 );
}
Also here is a link to the boolean playerprefs script: http://www.unifycommunity.com/wiki/index.php?title=BoolPrefs
Thanks.