- Home /
How to use PlayerPrefs to stop an item from reappearing when you return to the level
I'm very new to the concept of PlayerPrefs, as the code below probably demonstrates. I've looked around for an explanation to using PlayerPrefs to stop an item from reappearing, but I'm still stumped.
I'll make the scenario very simple: I have two levels in my game, Level 1 and Level 2. There's a pickup in Level 1 (which I'll call Pickup1), and I don't want this pickup to reappear if I pick it up in Level 1, go to Level 2, and then come back to Level 1. (I also don't want it to come back when I quit the game and restart, which is why I'm using PlayerPrefs and not DontDestroyOnLoad). How can I use PlayerPrefs to stop the pickup from reappearing?
Here is the code that I tried, but it's not working. I have this code set to my character in both Level 1 and Level 2. My guess is that there are at least 20 things wrong with my strategy. Thanks so much for your help--maybe one day I won't be a noob!
using UnityEngine; using System.Collections;
public class Playerprefscript : MonoBehaviour {
public GameObject Pickup1;
void Start()
{
PlayerPrefs.SetInt ("Pickup1", 0);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pickup1"))
{
other.gameObject.SetActive (false);
PlayerPrefs.SetInt("Pickup1", 1);
}
}
void Update()
{
if (PlayerPrefs.GetInt ("Pickup1") == 1) {
Destroy (Pickup1);
}
}
}
YES!! I nearly have $$anonymous$$rs welling up in my eyes, I'm so happy. I threw PlayerPrefs out the window and watched the great $$anonymous$$ike Geig tutorial that Flaring Afro linked to.
Building off $$anonymous$$ike's scripts, I added in an object pickup script that would refer to the main Game Control script. This script does two things: it lets me save my item pickup count, and it lets me stop items I've picked up from reappearing. Posting the code in case it's helpful to anyone else.
The version of the script here lets me save the on/off status for 2 items named and tagged "Pickup1" and "Pickup2", but with enough copying and pasting I think I could save all 180 or so items I plan to put in my game.
There are 3 different scripts involved: Script 1: Object pickup script that I have attached to my character Basically, this script does 3 things when my character collides with a pickup: it adds to my item pickup count (stored in the Game Control Script that $$anonymous$$ike Geig sets up); it sets the game object to false; and it also changes the "state" (on/off status) of the item to 0. The update scripts below the OnTriggerEnter scripts tell the game to destroy the items if the states are at 0. Because all these values are stored in the GameControl script, they persist from level to level.
using UnityEngine; using System.Collections;
public class Objectpickuppersistence: $$anonymous$$onoBehaviour {
public GameObject Pickup1;
public GameObject Pickup2;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pickup1"))
{
other.gameObject.SetActive (false);
GameControl.control.Pickupcount +=1;
GameControl.control.Pickup1state = 0;
}
if (other.gameObject.CompareTag ("Pickup2"))
{
other.gameObject.SetActive (false);
GameControl.control.Pickupcount +=1;
GameControl.control.Pickup2state = 0;
}
}
void Update()
{
if (GameControl.control.Pickup1state == 0)
{
Destroy (Pickup1);
}
if (GameControl.control.Pickup2state == 0)
{
Destroy (Pickup2);
}
} }
Script 2: Game Control Script I got this script from $$anonymous$$ike Geig's tutorial. Ins$$anonymous$$d of "health" and "experience," my public integers are Pickupcount, Pickup1 state, and Pickup2state. (I could add hundreds of Pickupstate variables if needed.)
Script 3: Load and Save buttons This is the other script that $$anonymous$$ike Geig introduces. I took out the buttons for manually increasing and decreasing your stats, and just kept the load and save buttons.
I'm so relieved! The simpleton/static variable setup of the Game Control code makes it so easy to access my object count and state variables. I would hug all of you if I could. (Hugs Ethernet cable)
Answer by KuR5 · Jun 24, 2016 at 08:57 AM
I think that is due to set player prefs value to 0. So comment out PlayerPrefs.SetInt ("Pickup1", 0); in Start().
And use if (PlayerPrefs.GetInt ("Pickup1",0) == 1) in Update() or everywhere else you used before.
Amazing, it worked! All I did was delete the start code.
Now I have to figure out how to retain my item pickup count using playerprefs. I'm sure there are some good tutorials on that, but any tips would be appreciated.
Thanks again!
You shouldn't be using playerprefs for this. Anyone could easily modify what they've accomplished. Unity has a tutorial showing you a much better approach.
well as @Flaring Afro said you should not use the playerprefs its a easy target.I used the persitence tutorial and it saves the data in binary and not easily hackable. Thanks Flaring :)
Answer by KBurchfiel · Jun 24, 2016 at 10:39 PM
That sounds promising--would the alternative save method still work when you quit and restart the game, or does it only save the data while the game is running?
@$$anonymous$$Burchfiel yes it works because it stores value in binary format in storage and value persist even after restarting the game. go through the tuorial. cheeers :)
Your answer
Follow this Question
Related Questions
Is it safe to save values in a prefab instead of using PlayerPrefs? 0 Answers
Loading error when using PlayerPrefs.GetInt with WebPlayer 2 Answers
PlayerPrefs 2 Answers
PlayerPrefs don't load again after Android game closes for the first time. 1 Answer
Saving current scene in another scene during gameplay? 1 Answer