Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by KBurchfiel · Jun 24, 2016 at 08:37 AM · playerprefssavescenespickupitem pickup

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);
     }

 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KBurchfiel · Jun 25, 2016 at 07:27 AM 1
Share

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)

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KBurchfiel · Jun 24, 2016 at 04:59 PM 0
Share

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!

avatar image Flaring-Afro KBurchfiel · Jun 24, 2016 at 07:22 PM 1
Share

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.

https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data?playlist=17117

avatar image vsuraj25 KBurchfiel · Sep 13, 2020 at 12:57 PM 0
Share

can you show your working code

avatar image iamrahulmaurya · Jun 24, 2016 at 09:24 PM 1
Share

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 :)

avatar image
1

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iamrahulmaurya · Jun 24, 2016 at 11:43 PM 0
Share

@$$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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

50 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges