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
1
Question by spinnerbox · Mar 04, 2016 at 02:21 PM · c#unity 5serializationreferencesdeserialization

Unity still references file moved to different folder

I have this file called GameData.cs and I moved it from top level folder Assets.Scripts to Assets.Scripts.GameDataScripts. Just to mention I first made folder GameData which clashed with GameData.cs, then I renamed it to GameDataScripts.

I wrote this unit test to test if my function is loading the local disk file but it throws an error:

 [Test]
 [Category(Helper.TEST_CATEGORY_SAVE_GAME_STATE)]
 public void FileDataTest_ ()
 {
     // arrange
     var slgdController = FakeSaveLoadGameDataController();
     BinaryFormatter bf = new BinaryFormatter();
     FileStream fs = File.Open(Application.persistentDataPath + Helper.GAME_DATA_FILE_NAME, FileMode.Open);
         // act
     GameData expected = (GameData)bf.Deserialize(fs);
     GameData actual = slgdController.FileData();
     
     // assert
     Assert.AreEqual(expected, actual);
                 
     // clean
     fs.Close();
 }

Error:

 System.TypeLoadException : Could not load type 'Assets.Scripts.GameData'.

As if it is still referencing the the old GameData folder or file GameData.cs

What can I do to tell Unity3D to clear old file/folder references?

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

Answer by Bunny83 · Mar 04, 2016 at 04:33 PM

Your information here seems to be quite confusing. You talk about a "file" called "GameData.cs". The extension ".cs" would tell us this is a C# script, right? However all your code seems to just use the class "GameData" that is most likely defined in that script file to deserialize (most likely another) file which should represent an instance of that class.

You said you "moved" the script file. Did you move it inside Unity? Or did you move it in the explorer / finder or any other program? If you moved it outside of Unity, Unity will loose the asset connection to the script unless you moved the ".meta" file along. You should move assets only inside of Unity.

Next thing is the error message:

 System.TypeLoadException : Could not load type 'Assets.Scripts.GameData'.

doesn't reference a certain file, but your class name. You most likely have your GameData class declared in the namespace "Assets.Scripts". If you created the class in VisualStudio, that's what usually happens as VS places new classes in a namespace according to their folder names. Moving the file won't change the namespace.

If you changed the namespace yourself, that would mean you now using a completely different type. The BinaryFormatter stores the exact class type which includes the namespace and the assembly that contains the class. Just because a class has the same name as another class, doesn't mean it's the same class if they belong to different namespaces. Best examples are: System.Object and UnityEngine.Object or System.Random and UnityEngine.Random.

Whenever you change anything on your class that you serialize / deserialize with the BinaryFormatter, it will break any serialized data you've created already.

So make sure you place your classes in the final namespace you actually want to use before using the class for any serialization. Also keep in mind when adding / removing variables it will break the deserialization as well.

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 spinnerbox · Mar 05, 2016 at 01:00 PM 0
Share

The file is in the Unity folder in Assets.Scripts.GameDataScripts. POrobably I should clear the file being created previosly?

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

128 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

Related Questions

How to protect JSON file game data? 3 Answers

JsonUtility doesn't serialize nested mixed var 1 Answer

Multiple Cars not working 1 Answer

Coding help: How to use xml serialization 1 Answer

Distribute terrain in zones 3 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