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 Ziuber · Nov 23, 2015 at 05:03 PM · saveloadreferencing

How to protect SaveLoad script from failing?

Hi, I use a SaveLoad script where I use binary formatter to save an instance of a class. I have a problem when I add a variable to the saved class because, let's say: saved class contains a variable named variable1, then I edit the script and add variable2, then when I launch the game, the game loads save file. It loads variable1 and tries to load variable2 but variable2 is not yet present in save file and it crashes the whole game.

I basically need something like this:

 if (variable2 exists)
 {
 reference the variable
 }

I can't use

 if (variable2)
 {
 reference the variable
 } 

because variable2 is an integer and "can't be converted to boolean". Any ideas?

Here's the code I am using: http://pastebin.com/RjuTMrWw

Comment
Add comment · Show 1
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 xxZap · Nov 24, 2015 at 02:07 PM 0
Share

the if statement is not necessary. Is correct save and load all the class data, you don't need to check if a variable is or is not setted and if or not must be loaded/saved.

For solve the crash you may: 1) detect the file and delete it manually; or 2) comment "load" in the code; click play; let the game saves again the class (adding variable2 to the serialized file); stop; uncomment "load"; try to play;

1 Reply

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

Answer by Rostam24 · Nov 24, 2015 at 02:43 PM

Maybe you should use a Try Catch block for this..?

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 Ziuber · Nov 24, 2015 at 09:54 PM 0
Share

thank you. That fixed the problem.

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

36 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

Related Questions

how do i keep object destroyed after reloding scene ?? 0 Answers

Creating an instance of a prefab at start? 1 Answer

How To Save and Load player position using Serialization? 0 Answers

Save location of created UI elements to JSON so it can load later 0 Answers

Changing scene by clicking on a game object 1 Answer


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