Reflection GetValue SetValue on scripts. UnityScript.

Hello,
I’m using reflection to save my script variable values to a file (Save game), using UnityScript.
Everything is working as it should except for boolean variables.

I can write a script to a file, open the file and use Script.GetType()þGetField(varName).SetValue(Script, value) to load the data from the file into the script again. This works for all variables except for boolean.
SetValue() isn’t working. No boolean value is updated. No errors are being thrown.

A small snippet of my code : Its saving all the values into a string.

for(var val2 :String in Arr)
{
		name = val2.Substring(0,val2.IndexOf(":")); 	
		var Valu =val2.Substring(val2.IndexOf(":")+1);
		var Valu2;
		var count : int = 0;
		var FI= P.GetField(name);
		var val = FI.GetValue(Script);
		
		try
	  	{
	  		var Type11 : String= val.GetType().Name;
			Type11.Trim();
			switch(Type11) 
		    {
		        case "Int32": 
			        Valu2 = int.Parse(Valu);
			       	 FI.SetValue(Script,Valu2 );
		        break;
		        
		    	case "Boolean": 
		     
		        	print(FI.Name + " : "+ val.ToString());
		        	var bol : boolean = false;
		        	if(val.ToString() == "False")
		        	{
		        		 bol = false;
		        		
		        		 FI.SetValue(Script,bol);
		        		 print("Setting "+ FI + ": false ");	
		        	}
		        	else
		        	{
		        		bol = true;
		        		FI.SetValue(Script,bol);
                                }
                      }
}

Any Idea what might cause this?

                    if(val.ToString() == "False")

have you confirmed this is correct? in code true and false are lower case and if i remember correctly debug logs also print their strings this way.

anyway, as reflection is super slow i don’t know if its a valid method for doing savegames. there are 2 free packages from whydoidoit and marrrk to store data.

Looking over your code I saw this:

var val = FI.GetValue(Script);

You then proceed to use val in the boolean case, which does not seem right. Your code basically says:

Read the value of the field to val.
Write val to the same field.

You should use Valu and not val.

Yes!
That was one of the issues. Valu and Val, and that led me to another issue with my code.
All fixed now.
Thanks!

This will also fail in iOS by the way… just giving you a head’s up. Depending on whether you’re targeting full framework or subset. Instead of using GetValue and SetValue… the object you’re executing those on is a methodinfo. If the thing you’re setting is a Field then you’re fine… but if it’s a property it’s going to puke so you’ll need to use GetGetMethod and GetSetMethod and then call Invoke on those to set the value to ensure compatibility cross platform.