Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Is there a way to automatically reset public variables in script components?

Discussion in 'Editor & General Support' started by beartaco, Jul 12, 2021.

  1. beartaco

    beartaco

    Joined:
    Jul 6, 2021
    Posts:
    2
    In order to update changes I've made to public variables in my IDE I have to go to each component in the Unity client and hit Options > Reset. This is getting frustrating as I add more modules and I have to remember which components I've modified since last running the game, then go and manually reset each one.

    Is there a setting I'm missing in the Unity Client that will automatically take the updated public variables from the script files? Alternatively, is there a hotkey I can use that will reset every script component in the scene?

    Thanks.
     
  2. LethalInjection

    LethalInjection

    Joined:
    Jan 25, 2015
    Posts:
    36
    if you update the public variable after the play button has been hit, then when game stopped values will revert back to what they were before you started play.

    Other solutions require scripting in OnEnable/OnDisable or an editor script saving the values.
     
  3. beartaco

    beartaco

    Joined:
    Jul 6, 2021
    Posts:
    2
    My issue occurs while I'm not running anything, that is before I've hit the play button.

    A little bit more googling lead me to this thread (https://forum.unity.com/threads/components-reset-from-script-in-c.109429/) which includes some code that probably does the trick but it seems like a somewhat convoluted workaround for the simple issue I'm having.
     
  4. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,184
    So you mean, you have a script field like
    public int myNumber = 4;
    , then play the game, but decide, you want it to be
    public int myNumber = 5;
    ?

    This sounds like the common issue I see with developers coming from other environments. In Unity, the norm is to serialize data and edit it in the inspector (public fields or private fields decorated with the SerializeField attribute), or assign variables in script (private fields without attribute, public field with NonSerializedAttribute or public auto-property). Mixing both approaches can lead to confusion and other issues. For example, it is the intended behaviour that the value assigned during static variable initialization (my example code above) defines the default values for serialized data. But from there on out, the values defined in the editor will always take precedence. This makes sense for the Unity system because of the way you can use MonoBehaviour scripts in multiple scenes, prefabs and also with override functionality.

    So, in short, I would recommend to just set a reasonable default value in script, then edit the data in Unity. Now you have a different problem to solve. That of multi-editing or batch processing with editor scripts.

    On the other hand, if you prefer defining everything in code and waiting for recompile, you can always make you variable NonSerialized, private or a property so it won't be serialized in the editor.

    There might be some reasons why you want to reset your default value and also overwrite all serialize data in all scenes and prefabs with the new value, but that really should be a non-regular thing and is usually solved with custom editor tooling. There's also a feature request open for Unity to provide an official solution for "data upgrades", but really, in your case it sounds like you should stick to one of the paradigms.
     
    lancelotpoulin likes this.