Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by AnneyYe · Jan 19, 2019 at 03:13 AM · listsave data

Help! my list won't remove its elements...

So, the story is, I am trying to create a collection system for my RPG that, e.g. once it first reaches a scene, it will "unlock" a picture, and I tried to do this by setting up a list and a function that once it runs, it will first add a string element (which is the name of a game object) to the list, then save it locally. And I plan to extract info from the string by going through the string elements and show the game objects with names that are equivalent to the strings (so find game object with name string).

So as a coding beginner, I wrote the code for adding and saving string, as below:

using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using System; public class SCP : MonoBehaviour { public static SCP controlClt;

 public List<String> meCltName = new List<String>();//I should put this elsewhere that would only run in the beginning of the game?

 private void Awake()
 {

     if (controlClt == null)
     {
         DontDestroyOnLoad(gameObject);
         controlClt = this;
     }
     else if (controlClt != this)
     {
         Destroy(gameObject);
     }

     if (File.Exists(Application.persistentDataPath + "/collectionInfo.dat"))
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Open(Application.persistentDataPath + "/collectionInfo.dat", FileMode.Open);
         CP data = (CP)bf.Deserialize(file);
         file.Position = 0;
         file.Close();
         meCltName = data.meCltName;//the list is saved into data---this is just modification from the previous save tutorial code
     }
 }


 public void addtoCltName(GameObject me)//this function will add a game object input to collection and also save it in folder
 {
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create(Application.persistentDataPath + "/collectionInfo.dat");

     CP data = new CP();
     String mee = me.name;
     meCltName.Add(mee);//add the inputed gameobject name to the list meCollection
     data.meCltName = meCltName;

     bf.Serialize(file, data);
     file.Close();
 }

 [Serializable]
 class CP
 {
     public List<String> meCltName;
 }

But when I tested out weird things happened. At first I didn't really know how it works so when I first dragged the script to a game object I set the list size as 20. Then I created a button and put the script of adding and saving the string onto it. When I clicked on the button I realized that it indeed added a new element, to the end of the list every time I clicks it, and the size increases. However, I want to start over and remove all those 20 empty game objects. However when I tried to resize it to, e.g. 0, after I exited play mode, it will show to be resized but once I entered the game the list returned to how it was last time. I closed the editor and restarted it but the 20+ elements were still there, and once an element is added when I click the button in play mode, it won't go away.alt text

2019-01-18.png (333.8 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

168 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

Related Questions

How can I save this objects ? 0 Answers

Saving And Loading List error 1 Answer

Serialize a list to save it 3 Answers

Problem to add on List (Serializable class) 2 Answers

How to get tooltips to pop up on certain levels, but not all of them 2 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