- Home /
store data locally to handle items/enemies appear only once
Storing data locally between scenes problem: I have a object class that persists and keeps track of my variables that I want to keep (special items, score, etc..).
My problem is I have 3 carrots and when I pick up only 1 and change scene, come back and the 3 are gone. How can I target only the one I collided with?, also is this a good approach?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickup : MonoBehaviour {
public bool isGem;
public bool isHeal;
public bool isCarrot;
public bool wasCarrotDestroyed = false;
public GameObject ExplosionFX;
//to prevent duplication of method if we have
//2 colliders colliding with obj at same time.
private bool isCollected;
// Start is called before the first frame update
void Start() {
if(LocalSaveDataManager.instance.wasCarrotDestroyed) {
Destroy(gameObject);
}
}
private void OnTriggerEnter2D(Collider2D other) {
if (other.CompareTag("Player") && !isCollected) {
if (isCarrot) {
isCollected = true;
Destroy(gameObject);
wasCarrotDestroyed = true;
// Save this info
LocalSaveDataManager.instance.wasCarrotDestroyed = true;
LocalSaveDataManager.instance.carrotCounter += 1;
}
}
}
}
//This is the game object keeping track of local data using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LocalSaveDataManager : MonoBehaviour {
void Awake() {
if(instance == null) {
DontDestroyOnLoad(gameObject);
instance = this;
} else if(instance != this) {
Destroy(gameObject);
}
}
public static LocalSaveDataManager instance;
//fields to store our local saved data
public int HP;
public int carrotCounter;
public bool wasCarrotDestroyed;
}
Answer by Gon1989 · Apr 14, 2021 at 02:59 AM
So I have been reading and I think Dictionary would be the smart way to keep data in different scenes and keep destroyed or track of any gameobject I want.
Im struggling to keep track of Coin collectable this is what I have:
public class Pickup : MonoBehaviour {
public bool isCarrot;
public bool wasCarrotDestroyed = false;
public int CoinId;
private bool isCollected;
// Start is called before the first frame update
void Start() {
}
private void OnTriggerEnter2D(Collider2D other) {
if (other.CompareTag("Player") && !isCollected) {
if (isCarrot) {
isCollected = true;
LocalSaveDataManager.instance.checkCollectibles(
this.CoinId);
Debug.Log("id: " + CoinId);
LocalSaveDataManager.instance.carrotCounter += 1;
Destroy(gameObject);
}
}
}
}
So far im passing a manually id assign by me in the item.
In data persistence class is my problem, I don't know how to add the id to a dictionary so I can keep track of it and then destroy if it was collected already.
public class LocalSaveDataManager : MonoBehaviour {
public static LocalSaveDataManager instance;
public bool collected;
//fields to store our local saved data
public int carrotCounter;
public bool wasCarrotDestroyed = false;
public static Dictionary<int, bool> coinCollectedDatabase;
private bool carrot1 = false;
private bool carrot2 = false;
private bool carrot3 = false;
private void Start() {
}
public void checkCollectibles(int id) {
if(coinCollectedDatabase.ContainsKey(id)) {
if (coinCollectedDatabase[id]) {
Destroy(gameObject);
} else {
coinCollectedDatabase.Add(id, false);
}
}
}
void Awake() {
if(instance == null) {
DontDestroyOnLoad(gameObject);
instance = this;
} else if(instance != this) {
Destroy(gameObject);
}
}
}
Your answer
Follow this Question
Related Questions
Is there any way to restore previous save of C# script 0 Answers
Saving progress even when I quit the windows app 0 Answers
Save Load Data unity 0 Answers
Simple way to save large files 0 Answers
Saving Data on platform cloud 0 Answers