Search Unity

Unity Serializer won't save variable changes?

Discussion in 'Scripting' started by asdx, Nov 24, 2013.

  1. asdx

    asdx

    Joined:
    Jun 3, 2013
    Posts:
    18
    Hello guys. I've tried using serializer but I can't seem to make it save the changes I make to the variables.
    I declared some simple variables.. This is the code:
    Code (csharp):
    1. private int x = 3;
    2.     bool geo = true;
    3.     Vector3 pozV3;
    4.     Vector2 pozV2;
    5.            
    6.     void Start()
    7.     {
    8.         pozV3 = transform.position;
    9.         pozV2 = transform.localRotation.eulerAngles;
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         print (x+" "+geo+" "+ pozV3+" "+pozV3);
    15.        
    16.         if(Input.GetKey(KeyCode.L))
    17.         {
    18.             x = 2; geo = false;
    19.         }
    20.     }
    When I click play it prints the default values. Then I press the L key and the values change accordingly. I save and then load.. But upon loading the values are reverted back to default.. What am I missing? Anyone know please?

    EDIT: Ok.. found out that private variables don't get saved unless specified.. but I tried a public Vector2 attached to the camera and that doesn't seem to work..
     
    Last edited: Nov 24, 2013
  2. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    yes a vector2 field change made in playmode will not serialize.

    Somebody has made an editor extension on the asset store that facilitates serializing data while in playmode. I dont know to what extent it works or not..
     
  3. Mycroft

    Mycroft

    Joined:
    Aug 29, 2012
    Posts:
    160
    Is this an issue that's become apparent now that you serialized the data so you can modify it with a custom Inspector?

    If so the issue is probably in the Inspector code; you need to tell the Editor (with EditorUtility.SetDirty) that the data on the script has changed.

    Code (csharp):
    1.  
    2. [CustomEditor (typeof(UserQuizControl))]
    3. public class UserQuizControlEditor : Editor {
    4.    
    5.    
    6.     private UserQuizControl myTarget;
    7.     private int Length = 0;
    8.  
    9.         void Awake()
    10.     {
    11.         myTarget = (UserQuizControl) target;
    12.         Length = myTarget.QuizQuestions.Length;
    13.         ...
    14.     }
    15.  
    16. public override void OnInspectorGUI() {
    17.        
    18.         Length = EditorGUILayout.IntField( "Size",  Length );
    19.         EditorUtility.SetDirty(target);
    20. }
     
    Last edited: Nov 25, 2013