passing manipulating PlayerPrefs data

Hello all,

I hope I can get ideas from the community on how to get certain things to work since I’m still new to Unity.

I’m working on a project using the code from the Lerpz tutorial due to time constraints + I’m doing the coding (not an expert programmer) art.

I’ve got everything working as I’d like; but I need to change some scripts because the tutorial involves one scene & I’d like to have mine broken up into multiple scenes.

1st off these are the 3 scripts I’m working on:

  1. LevelStatus.js attached to the level GameObject.
    // LevelStatus: Master level state machine script.
    // This is where info like the number of items the player must collect in order to complete the level lives.

I added the following function:

function Update()
{
//stores the number of items to be accessed by the HUD
PlayerPrefs.SetInt(“ITEMS”, itemsNeeded);
}

  1. GameHud.js
    // GameHUD: Platformer Tutorial Master GUI script.
    // This script handles the in-game HUD, showing the lives, number of fuel cells remaining, etc.

  2. ThirdPersonStatus.js
    // ThirdPersonStatus: Handles the player’s state machine.
    // Keeps track of inventory, health, lives, etc.

That said- this is what I need to happen.

Pass manipulate itemsNeeded data so that the HUD is updated when a new scene is loaded. I’d like to the same with player health.

I’m having a hard time where I should put the function to do this. I’d like to ask the community for some help on this since I’ve no one to ask regarding code.

(attached a zipped file with scripts for reference)

Thanks in advance,
scr33ner

1081245–40460–$scriptsUsed.zip (5.14 KB)

PlayerPrefs.SetInt(“ITEMS”, itemsNeeded);

The first parameter of the pref should be a string value “SomePrefKey” . The key is a lot like naming a var so that your game can find your save state.

The second parameter just holds your var (Int in this case) . You only need to call it once on Start() and once on OnApplicationQuit() . It’s best to create your own functions for this (ie) SaveIt() LoadIt() then call those functions in Start() and OnApplicationQuit() .

To assign a pref on Start()/LoadIt() a pref most the time looks like this (eg) itemsNeeded = PlayerPrefs.GetInt(“SomePrefKey”);

Good Luck

thanks mantis, will give this a go…