- Home /
Deleting PlayerPrefs is killing my player
EDIT: Adding some scripts in hopes of helping solve the problem
EDIT 2: Ok, so I noticed that in disabling the playerhealth script I get a bunch of errors popping up from PlayerPrefsX after deleting PlayerPrefs. I wonder if maybe an end around to all this is to simply disable the load function on the save script when choosing "New Game"
Edit 3: In the save script there is a boolean for the load function that I can set to true or false in the inspector. I've been defaulting to it always being true, but maybe I'm thinking of this all backwards. Maybe I need to set it to false, and forget about deleting playerprefs. Then, when new game is selected, the script simply doesn't load the previous data. When load is selected, the load boolean is set to true, and the game loads. This brings up another (probably simple) issue: can I call the load function, even though that script rests on a gameobject in another scene?
Hello everyone.
Let me start out with this issue is probably occuring somewhere in any number of scripts, so I'm not sure which of my scripts to post here. I suppose my hope is that I can ask the question clearly enough and somebody may have had a similar issue in the past. Here goes:
Am getting close to finishing my game, and have placed the main menu. In the menu the player can choose "New Game - Continue - Exit." So, continue and exit work fine, but New Game is giving me a very strange bug. Everything in the game is saved via playerprefs: Player position, level, enemies killed, MAX player health, (not current though)etc. Now, New Game works by asking if the player wants to delete any saved data, and they have a yes / no option screen. (script wise I used PlayerPrefs.DeleteAll();) If they choose yes any playerprefs are deleted and the game's intro begins. But then something REALLY weird happens: Upon game startup the player is instantly killed. Like, hp reduced to 0, game over screen comes on. So, I disabled the playerhealth script and found on next startup that the player was now still "alive" but was way under my map, not at where they should be at all on start. (and falling, obviously) I thought maybe the reason they were dying, therefore, was that they were hitting the emergency collider that I keep under the map, but when I made that to transfer, rather than to kill the player, they still died. (once the player health script was reenabled) So I'm at my wit's end. Player health sets perfectly if I just gametest from the main game scene, but if I go from the main menu and select new and allow playerprefs to get deleted, health always starts at 0. Again, I'm not even sure which code(s) to post since I'm not sure if it's the save / load script, the delete playerprefs script, or the health script (or none or all of the above) being affected. Your patient, understanding help is greatly appreciated. Thanks, and God bless.
Save Script:
#pragma strict
var inventoryScript : Inventory2;
var saveGame : boolean = false;
var loadGame : boolean = false;
// private var
private var playersLastPosition : Vector3;
function Start ()
{
inventoryScript = GetComponent(Inventory2);
}
function Update ()
{
if(saveGame == true)
{
SaveGame();
}
if(loadGame == true)
{
LoadGame();
}
}
function SaveGame()
{
for(var x = 1; x>0; x++)
{
yield WaitForSeconds(5);
PlayerPrefsX.SetIntArray("Ammount in inventory", inventoryScript.ammountInInventoryArray);
PlayerPrefsX.SetVector3("PlayersPosition", gameObject.transform.position);
PlayerPrefs.SetInt("CurXp", Playerhealth.curXp);
PlayerPrefs.SetInt("MaxXp", Playerhealth.maxXp);
PlayerPrefs.SetInt("MaxHealth", Playerhealth.maxHealth);
PlayerPrefs.SetInt("CurHealth", Playerhealth.maxHealth);
PlayerPrefs.SetInt("Level", Playerhealth.level);
PlayerPrefs.SetInt("Money", Playermoney.curMoney);
PlayerPrefs.SetInt("Club", WeaponInv.Club);
PlayerPrefs.SetInt("Sword", WeaponInv.Sword);
PlayerPrefs.SetInt("Camera", WeaponInv.Camera);
PlayerPrefs.SetInt("SoulReaver", WeaponInv.SoulReaver);
PlayerPrefsX.SetBool("weaponready", Player3Weapons.weaponready);
PlayerPrefsX.SetBool("swordready", Player3Weapons.swordready);
PlayerPrefsX.SetBool("cameraready", Player3Weapons.cameraready);
PlayerPrefsX.SetBool("obtainedWeapon02", Player3Weapons.obtainedWeapon02);
PlayerPrefsX.SetBool("obtainedWeapon03", Player3Weapons.obtainedWeapon03);
PlayerPrefsX.SetBool("obtainedWeapon04", Player3Weapons.obtainedWeapon04);
PlayerPrefsX.SetBool("obtainedWeapon05",Player3Weapons.obtainedWeapon05);
}
}
function LoadGame()
{
inventoryScript.ammountInInventoryArray = PlayerPrefsX.GetIntArray("Ammount in inventory");
gameObject.transform.position = PlayerPrefsX.GetVector3("PlayersPosition");
Playerhealth.curXp = PlayerPrefs.GetInt("CurXp");
Playerhealth.maxXp = PlayerPrefs.GetInt("MaxXp");
Playerhealth.maxHealth = PlayerPrefs.GetInt("MaxHealth");
Playerhealth.curHealth = PlayerPrefs.GetInt("CurHealth");
Playerhealth.level = PlayerPrefs.GetInt("Level");
Playermoney.curMoney = PlayerPrefs.GetInt("Money");
WeaponInv.Club = PlayerPrefs.GetInt("Club");
WeaponInv.Sword = PlayerPrefs.GetInt("Sword");
WeaponInv.Camera = PlayerPrefs.GetInt("Camera");
WeaponInv.SoulReaver = PlayerPrefs.GetInt("SoulReaver");
Player3Weapons.weaponready = PlayerPrefsX.GetBool("weaponready");
Player3Weapons.swordready = PlayerPrefsX.GetBool("swordready");
Player3Weapons.cameraready = PlayerPrefsX.GetBool("cameraready");
Player3Weapons.obtainedWeapon02 = PlayerPrefsX.GetBool("obtainedWeapon02");
Player3Weapons.obtainedWeapon03 = PlayerPrefsX.GetBool("obtainedWeapon03");
Player3Weapons.obtainedWeapon04 = PlayerPrefsX.GetBool("obtainedWeapon04");
Player3Weapons.obtainedWeapon05 = PlayerPrefsX.GetBool("obtainedWeapon05");
inventoryScript.displayChanged = true;
loadGame = false;
}
First Menu Script:
var ammountInInventoryArray : int[]; // Holds how many items you have.
var itemNameArray : String[]; // The name of the array set in the hierarchy.
var onScreenText : GameObject[]; // Holds your gui text.
var numberOfItems : int = 6; // Total number of items in the game. Start counting this at zero. For example if you have 10 items then this number needs to be 9
// var selectedTextColor : Color; // Was suppose to let you selcet your text color. Couldn't get it to work :(
// private var
private var numberOfItemsDisplayed : int = 0; // These all keep your script running fine. I wouldn't mess with these unless you fully understand the script
private var itemPosition : int = 0;
private var colorCheck : int = 0;
private var currentPosition : GameObject;
private var upCheck : boolean = true; // Checks to see if your still holding up
private var downCheck : boolean = true; //Checks to see if your still holding down
// global variables
static var displayChanged : boolean = true; // Set this to true everytime there is an inventory change. It will auto set back to false once inventory is updated
static var selectingInventory : boolean = true; // Set this to true when your accessing your inventory in game. Set it to false when you don't want to access your inventory.
function Start()
{
}
function Update()
{
if(selectingInventory == true)
{
SelectItemFromInventory(); // During the game if you want to access your inventory make sure selectingInventory is true.
}
if(displayChanged == true)
{
// Every time you add or take an item from inventory you should set displayChanged to true.
//This will keep the number of items you have up to curret without calling it every frame all game long.
DisplayItems();
}
}
function SelectItemFromInventory()
{
if(itemPosition < 0)
{
itemPosition = numberOfItems;
while(onScreenText[itemPosition].active == false)
{
itemPosition--;
}
}
if(itemPosition > numberOfItems)
{
itemPosition = 0; // Makes you go back to top if you hit the bottom of the list
print("this happened");
while(onScreenText[itemPosition].active == false)
{
itemPosition++;
if(itemPosition > numberOfItems)
{
break;
}
}
}
if(Input.GetAxis("Vertical") < -.5)
{
if(downCheck == true)
{
// moves you down an item
itemPosition++;
colorCheck = 0;
ResetTextColor(); // Sets text back to white
downCheck = false;
}
while(onScreenText[itemPosition].active == false)
{
itemPosition++; // this makes sure you don't stop on an empty item
if(itemPosition > numberOfItems)
{
break;
}
}
}
else
{
downCheck = true;
}
if(Input.GetAxis("Vertical") >.5)
{
if(upCheck == true)
{
// Moves you up the list
itemPosition--;
colorCheck = 0;
ResetTextColor();
upCheck = false;
}
if(onScreenText[itemPosition].active == false)
{
itemPosition--;
while(onScreenText[itemPosition].active == false)
{
itemPosition--;
if(itemPosition < 0)
{
break;
}
}
}
if(itemPosition < 0)
{
itemPosition = numberOfItems; // makes you go to the bottom if you hit the top of the list
}
}
else
{
upCheck = true;
}
currentPosition = onScreenText[itemPosition];
currentPosition.guiText.material.color = Color.magenta;// Changes the color so you know what item your on
if(Input.GetButtonUp("Action"))
{
//These select your items.
if(itemPosition == 0){
PlayerPrefs.DeleteAll();
Application.LoadLevel(6);
}
if(itemPosition == 1){
Application.LoadLevel(0);
}
}
}
function DisplayItems()
{
if(numberOfItemsDisplayed <= numberOfItems)
{
if(ammountInInventoryArray[numberOfItemsDisplayed] <= 0)
{
onScreenText[numberOfItemsDisplayed].active = false; // turns items off if there isn't any in the inventory
}
else
{
onScreenText[numberOfItemsDisplayed].active = true; // turns items back on if they are in the inventory
onScreenText[numberOfItemsDisplayed].guiText.text = itemNameArray[numberOfItemsDisplayed]; // Displays the name and current amount in stock
}
numberOfItemsDisplayed++;
}
else
{
// Turns off this function so that it doesn't always play. Make displayChanged true when you need it back on.
displayChanged = false;
numberOfItemsDisplayed = 0;
}
}
function ResetTextColor()
{
while(colorCheck <= numberOfItems)
{
onScreenText[colorCheck].guiText.material.color = Color.white;
colorCheck++;
}
}
First: can you reduce your code to the $$anonymous$$imum necessary to reproduce the problem? It's certainly quite a lot of code what you posted here. Second: you should not use PlayerPrefs for most things you are using it. Check this: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading
it is a lot of code, and I apologize. I went from the extreme of not having any code, to throwing everything at the wall to see what worked. Still not entirely sure which is most appropriate. Thank you for the link. Am hoping to not go "scorched earth policy" by having to remove my playerprefs this late in the game, but that's coding sometimes, right? All this said, I think there might be a fairly easy solution in just having the save / load script load function disabled on game start, and only enabling it on the player selecting "Continue."
Answer by Conect11 · Mar 10, 2014 at 08:32 PM
The answer ended up being pretty simple. In the "First Menu Script" I had to set my ints for player position and player health. Once I did that, the issue went away.
Answer by suribe · Mar 10, 2014 at 08:20 AM
From what I see here, which does not show how the savegame is called, I would guess the problem is:
function SaveGame()
{
for(var x = 1; x>0; x++)
{
...
That will loop on forever. Starts with x = 1, then increments, x = 2, etc. and x>0 will always be true, so this never ends. Remove that loop. Likely, as soon as you set saveGame = true, the Update function gets stuck at SaveGame and never calls LoadGame... or some other weird thing might happen, depending on how you call this script.
Also, why call them from Update? That gets executed every frame. Just call the functions (SaveGame, LoadGame) directly from other script when you need to save or load.
Your answer