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