Search Unity

Set a script property as changed ( so that it is saved with scene )

Discussion in 'Scripting' started by grobonom, May 7, 2021.

  1. grobonom

    grobonom

    Joined:
    Jun 23, 2018
    Posts:
    335
    Hi all.

    I confess i'm kinda lost with those serialization and properties concepts. And i find no solution on my problem.

    I'm sure most of you will laugh at me as i guess the solution is obvious; so here's my problem:

    1) usual things:
    You create a c# script with publics. You put it as a component on you gameObject and it works fine with all defaults set up in the script....
    here's my script: (huhuhuh)
    Code (CSharp):
    1.  
    2. public class FieldTranslator : MonoBehaviour
    3. {
    4. [SerializeField]
    5.    public int uniqueID=-1;
    6.    public bool locked=false;  
    7. }
    Now you change the value for the uniqueID property...
    Unity3D marks this property in bold ( meaning it has been changed from its default value )
    The same thing happens when you instance the gameObject holding this script from a prefab and when you change the value so that it's different from the prefab one.

    I'm okay with all this.
    but now..... hehehehe :p

    I create a customInspector that customizes my inspector :p but that also changes the uniqueID !

    And this is where i fu*k things up.

    everything works like a charm. Each of my gameObjects has its unique ID....
    Save scene, reload and all uniqueID go back to -1 :eek:

    All this because the change of uniqueID from a script does not mark the property as changed !

    If i change this property by hand, the changes are kept.

    So my question is quite simple:
    How to change a property with script and make it 'dirty', or 'changed', or 'MustBeSaved' ?

    Thanks a lot for your answers and happy unitying !
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
  3. grobonom

    grobonom

    Joined:
    Jun 23, 2018
    Posts:
    335
    finally i did:
    Code (CSharp):
    1.          // this is for forcing save of the modified ID
    2.          EditorUtility.SetDirty(FT);
    3.          Undo.RecordObject(FT, "Changed UID");
    4.          PrefabUtility.RecordPrefabInstancePropertyModifications(FT);
    ( FT beeing the script component in wich there's an INT i need to save... )
    It appears that the later line really does the job as the 2 first ones don't force modified values to be saved.

    Finally what i wanted works fine, even if i do not really know/understant why :p

    Happy unitying !
     
  4. SoumyaRipan86

    SoumyaRipan86

    Joined:
    Aug 19, 2019
    Posts:
    12
    Thank you much. I have been tired of searching a solution for hours. And finally, these three lines solved it for me. (In my case remember to use the component on the object whose parameters you want to save not the gameobject as method param (like FT))
    Such relief.
     
    grobonom likes this.