Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by JayFitz91 · Nov 05, 2014 at 07:05 PM · objectactiveplayerpref

issue with PlayerPref destroying all objects

I have some objects in my scene, what I would like is for them to disappear when I collect them and have the remain collected (not-active) when I restart the scene.

I'm trying to accomplish this using a playerPref, I have each individual object with this script on it, when the PlayerPref is at 0, I want it to be in the scene, when at 1, the object should be gone.

So far this works but only for all objects, when I collect 1 object, all other objects with this script in my scene disappear. If anyone could point out a solution, I would appreciate it.

EDIT

pdated the code to make it a bit tidyer, same issue still persists though

     using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class gemRemove : MonoBehaviour
 {
     private int collected = 0;
     
     void Start()
     {
         //PlayerPrefs.DeleteAll();
     }
     
     void OnCollisionEnter(Collision collide)
     {
         if(collide.gameObject.tag == "Player")
         {
             collected = 1;
             PlayerPrefs.SetInt("collected", collected);
         }
     }
     
     void Update() 
     {
         
         if(PlayerPrefs.GetInt("collected") == 1)
         {
             Destroy(gameObject);
         }
 
         if(Input.GetButtonDown ("Fire2"))
         {
             Application.LoadLevel (0);
         }
     }
 }
Comment
Add comment · Show 3
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 NaRango4 · Nov 06, 2014 at 12:28 PM 0
Share

i am guessing every gem object has this script attached, but there are saving with the same name. you should be saving as gem1, gem2,.. [which by the way I don't recommend saving thousand variables, create algorithm that does this]

since, all of them uses "collected" variable, every one disappers

avatar image Mayank Ghanshala · Nov 06, 2014 at 12:33 PM 0
Share

Quick question. Why you need PlayerPrefs for this? when it could be done by any boolean value too.

avatar image JayFitz91 · Nov 06, 2014 at 04:11 PM 0
Share

Hi, I use PlayerPrefs as I want the gems that i've collected to remain collected when the player re-enters the level. Or when the player quits the game

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baste · Nov 06, 2014 at 01:18 PM

Okay, so the playerprefs doesn't differentiate between where the save command is called from. So if you save the int "collected" as 1, the only thing that shows up in the player prefs is

collected: 1

So, every object you have the script attached to checks the same collected variable.

The best way to fix this is to give each gem a unique identifier, and throw that in with the name of the variable you save:

 public class gemRemove : MonoBehaviour
  {
      public int identifier;
      
      void OnCollisionEnter(Collision collide)
      {
          if(collide.gameObject.tag == "Player")
          {
              PlayerPrefs.SetInt("collected" + identifier, 1);
          }
      }

      void Update() {
          if(PlayerPrefs.GetInt("collected" + identifier) == 1)
              Destroy(gameObject);
      }


That should fix your problem.

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 JayFitz91 · Nov 06, 2014 at 04:14 PM 0
Share

Hi, Im having the same issue with the above, all gems start with the identifier as 0, is there a way I can give each gem a unique identifier?

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

29 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

Related Questions

Destroy and instantiate far objects 2 Answers

i want the player to match 5 items correctly before next scene appears. 1 Answer

Any way to check if an object is active? 1 Answer

Activating Game Objects 3 Answers

SetActive won't work 3 Answers


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