- Home /
PlayerPrefs Problem crashing with SetBool
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.
Wow dude, you seriously need to learn how to use arrays... Copy-pasting code is a sin.
Answer by Heratitan · Jul 28, 2010 at 02:12 AM
Ok, I found your problem. What you did is you put your variables LE1, LE2... into your script, representing your playerprefs. What you need to do is put the playerprefs directly in like so:
GUI.enabled = PlayerPrefsX.GetBool("LevelOneEnabled");
if (GUI.Button(Rect(120,100,40,40), (GUIicon1)))
{
Application.LoadLevel(11);
}
Hope that helps
Answer by sooryan · Jan 08, 2013 at 11:44 AM
How to do this with arrays.I Have around 50 levels
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]
Under the answer where it says edit | delete | more , click on more , then convert to comment
Also you don't have to wait for a moderator to approve a comment.
Your answer
Follow this Question
Related Questions
Set boolean to true from another scene 1 Answer
Purchase Level Unlock System 1 Answer
Who can help me with lock/unlock and hint/coin systems? 0 Answers
stumped on level unlock 0 Answers