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 /
avatar image
0
Question by fraeri · Dec 07, 2018 at 10:23 AM · unityeditorserializationsave datavisualstudio

Can't use .Add-Function for List

Hi, I'm struggling with Adding new Elements to a List that get's serialized afterwards. Getting a bit confused here. I have a SaveManager.cs storing a List-Class and the Functions for Saving and loading:

 public class SaveManager : MonoBehaviour {
 
     public static SaveManager control;
 
     public class Error
     {
         public float X; public float Y; public float Z;
 
         public Error( float newX, float newY, float newZ)
         {
             X = newX; Y = newY; Z = newZ;
         }
     }
 
     public List<Error> ErrorList = new List<Error>();
 
     [Serializable]
      public class ListContainer
     {
         public List<Error> error;
     }
 
     void Awake()
     {
         if (control == null)
         {
             DontDestroyOnLoad(gameObject);
             control = this;
         }
         else if (control != this)
         {
             Destroy(gameObject);
         }
 
         Load();
     }
 
     public void Save()
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "/Data.dat");
 
         ListContainer lc = new ListContainer();
         lc.error = ErrorList;
 
         bf.Serialize(file, lc.error);
         file.Close();
     }
 
     public void Load()
     {
         if (File.Exists(Application.persistentDataPath + "/Data.dat"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/Data.dat", FileMode.Open);
             ErrorList = (List<Error>)bf.Deserialize(file);
             file.Close();
 
             Debug.Log("SaveFile Found and loadad");
         } else 
         {
             Debug.Log("No SaveFile Found. Started Clean"); 
         }
     }
 }



Then i have a script for adding the entrys to the list. And there is the problem. I can' using the Add-Function:

 public List<SaveManager.Error> ErrorList = new List<SaveManager.Error>();
 
 public void Create () {  
         float newX = ErrorX;
         float newY = ErrorY;
         float newZ = ErrorZ;
     
             ErrorList.Add(new Error( newX, newY, newZ)); //<- Error: The type or namespace name `Error' could not be found.
 }


Any idea why? I guess it's super obvious -.-

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
2
Best Answer

Answer by hexagonius · Dec 07, 2018 at 10:29 AM

It's new SaveManager.Error there too. Error is a nested class of SaveManager.

Comment
Add comment · Show 4 · 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
avatar image fraeri · Dec 07, 2018 at 10:48 AM 0
Share

F***. Thanks man. That was indeed obvious. What a shame.

avatar image Bunny83 fraeri · Dec 07, 2018 at 11:19 AM 0
Share

That's of course the main error at hand, however keep in $$anonymous$$d that your "ErrorList" that you create inside any other class has nothing to do with the "ErrorList" inside your Save$$anonymous$$anager class. You only serialize that list. Just because you declare another variable within another scope with the same name, doesn't mean they belong together.


This seems to be for logging purposes. You usually want to have one instance of your Save$$anonymous$$anager and have other classes use that instance to log / add their errors to the list of that Save$$anonymous$$anager instance. In most cases one would use either a singleton or a static list for this. It looks like you already have a singleton (sort of) but you're not using it?


Also note that your "Error" class isn't marked with the Serializable attribute.

avatar image fraeri Bunny83 · Dec 07, 2018 at 12:00 PM 0
Share

Yes, I am realizing that something is wrong. I went through this tutorial here about persistent Data and also this about Lists/Dictionarys. thought I can adapt it to save a whole class persistent. But I think this isn't the case. Or at least I'm saving it right, but did not add the values right.

What I want is to "fill up" the ErrorList in Save$$anonymous$$anager and save it. That's it. Can I kindly ask you for a solution or helping hand?

avatar image Bunny83 fraeri · Dec 07, 2018 at 11:23 AM 0
Share

Also note that your title and error don't belong to each other. There is no problem with Add. If you break up your line into:

 var err = new Error( newX, newY, newZ);
 ErrorList.Add(err); 

You will notice the error is in the first of those two lines, not in the second. Chaining several things into one line is fine, but if you have trouble interpreting an error you may want to break up that line into several seperate lines if possible which makes it easier to pinpoint the issue.

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

103 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

Related Questions

Serialized data show information after load 0 Answers

Saving data through FileStream DataInfo? Think i'm misunderstanding something. 1 Answer

When attempting to serialize and save a jagged array it gives me an error message 1 Answer

Help with serialization 0 Answers

Losing serialized data after play button pressed. 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