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 Kastenessen · Dec 23, 2015 at 10:21 PM · serializationsaveloadbinaryformatter

Save and Load Problem, Dont quite understand.

Hi, I have a problem.

I cant work out why I cant seem to load and unload a class with three lists in it.

It keeps giving me this cast error:

InvalidCastException: Cannot cast from source type to destination type. EndlessManagerScript.LoadChunk (Vector3 fileNamePos) (at Assets/GAME ASSETS/SCRIPTS/EndlessManagerScript.cs:298) PlayerControlScript.Update () (at Assets/_CUSTOM ASSETS/SCRIPTS/PlayerControlScript.cs:204)

My class is:

  [Serializable]
 public  class blockData  
     {
 
         public List<string> objectNamesToSerialize = new List<string>();
         public List<Int32> xPosToSerialize = new List<Int32>();
         public List<Int32> yPosToSerialize = new List<Int32>();
 
     }


My save method is:

           try
             {
 
                 Stream stream = File.Open(Application.dataPath + @"\Saves\Chunks\chunk " +  thisChunkFilenameNumber, FileMode.CreateNew);
                 BinaryFormatter bformatter = new BinaryFormatter();
 
                    data.objectNamesToSerialize = objectNamesToSerialize;
                     data.xPosToSerialize = xPosToSerialize;
                     data.yPosToSerialize = yPosToSerialize;
 
                     
                
                 bformatter.Serialize(stream, data);
                 stream.Close();
 
             }
             catch (UnauthorizedAccessException) { print("SAVING ERROR!!!"); } //many more exception might happen, check documentation
      
              }




And my loading script is just:

  public  void LoadChunk(Vector3 fileNamePos)
     {  
         
             
             {
                 Stream stream = new FileStream(Application.dataPath + @"\Saves\Chunks\chunk " + fileNamePos, FileMode.Open,FileAccess.Read);
                 
                BinaryFormatter bformatter = new BinaryFormatter();
 
                  data = new blockData();
  
        data.objectNamesToSerialize = ((List<string>) bformatter.Deserialize(stream));
 
             //  here is where I've tried lots of different ways of reading it, i even mucked around with 
             //  binaryReader and had the same problems.  It cant seem to cast back into a new
             //  instance of the blockdata class for some reason.
 
 
                   stream.Close();
                   print(data.objectNamesToSerialize.Count);
             
                 File.Delete(Application.dataPath + @"\Saves\Chunks\chunk " + fileNamePos);
              
             }
             }




Not sure what I'm doing wrong. Thought I'd be able to save a list and then reload the list as is. Could anyone help me please?

Comment
Add comment · Show 4
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 Statement · Dec 23, 2015 at 10:35 PM 0
Share

Try cast to blockData?

avatar image Kastenessen Statement · Dec 23, 2015 at 10:51 PM 0
Share

Yeah Ive tried stuff like:

data = ((blockdata)bformatter.deserialize(stream);

or

data = bformatter.deserialize(stream) as blockdata;

Same error, cannot cast to destination.

Don't know why.

avatar image Statement · Dec 23, 2015 at 11:01 PM 0
Share

What type does Deserialize return? ... GetType ()

avatar image Kastenessen Statement · Dec 23, 2015 at 11:04 PM 0
Share

it says:

emptyRoomGenScript+blockData UnityEngine.$$anonymous$$onoBehaviour:print(Object) Endless$$anonymous$$anagerScript:LoadChunk(Vector3) (at Assets/GA$$anonymous$$E ASSETS/SCRIPTS/Endless$$anonymous$$anagerScript.cs:297) PlayerControlScript:Update() (at Assets/_CUSTO$$anonymous$$ ASSETS/SCRIPTS/PlayerControlScript.cs:204)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Kastenessen · Dec 23, 2015 at 11:18 PM

I got it lol. Don't know why it didn't work before except i was using the same variable for creating the area as well. So all I did was change data to dat so they wouldn't conflict and it now seems to work I think.

Have to test it a bit more. The code i changed is:

blockData dat = bformatter.Deserialize(stream) as blockData;

thats all.

Comment
Add comment · Show 1 · 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 Kastenessen · Dec 24, 2015 at 12:20 AM 0
Share

Nah still doesn't work. The data variable is empty for some reason but the file has the right data in it. So its not reading or storing the data in my data variable. I really don't understand this. It should be simple.

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

35 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

Related Questions

How to add a new string in a list of strings AND serialize the list everytime a new string is added to the list? 1 Answer

How to add a new data to existing Binary file without erase the previous data? 0 Answers

Saving and Loading Issues Lists/SetActive/Etc 1 Answer

problem with loading data with serialization in the right time 0 Answers

Change Savedata without errors 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