- Home /
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);
}
}
}
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
Quick question. Why you need PlayerPrefs for this? when it could be done by any boolean value too.
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
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.
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
![](https://koobas.hobune.stream/wayback/20220613170812im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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