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
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;
Answer by Rostam24 · Nov 24, 2015 at 02:43 PM
Maybe you should use a Try Catch block for this..?
Your answer
