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 Gon1989 · Mar 03, 2021 at 03:12 AM · unity 5save data

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



Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

 
Comment
Add comment · 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

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

203 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 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 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 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 avatar image avatar image avatar image

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


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