x


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.

more ▼

asked Jul 27 '10 at 11:59 PM

Heratitan gravatar image

Heratitan
314 20 22 29

Wow dude, you seriously need to learn how to use arrays... Copy-pasting code is a sin.

Jul 28 '10 at 02:53 AM qJake
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Jul 28 '10 at 02:12 AM

Heratitan gravatar image

Heratitan
314 20 22 29

It did thanks. =)

Jul 28 '10 at 02:13 AM Heratitan
(comments are locked)
10|3000 characters needed characters left

How to do this with arrays.I Have around 50 levels

more ▼

answered Jan 08 at 11:44 AM

sooryan gravatar image

sooryan
1

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.

Jan 08 at 11:44 AM alucardj
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x322
x295
x240
x125
x17

asked: Jul 27 '10 at 11:59 PM

Seen: 1824 times

Last Updated: Jan 08 at 11:44 AM