Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

314
Views
Unity resets parameter value set with Editor script on play

I've set up a very simple Editor script in Unity 2020.2.1f1 that, upon pressing an Inspector button, should change the value of a specified parameter to a value set in the code.

public override void OnInspectorGUI()
{
    DrawDefaultInspector();

    StateObject s = (StateObject)target;
    if (s.objID == 0)
    {
        if (GUILayout.Button("Generate Object ID"))
        {
            GenerateID(s);
        }
    }
}

public void GenerateID(StateObject s)
{
    s.objID = DateTimeOffset.Now.ToUnixTimeSeconds();
}

This all works like it's supposed to. I press the button, the correct number appears in the field, and I'm happy. However, once I switch to Play mode, the value resets to the prefab default and remains that way even when I switch Play mode off.

Am I missing some ApplyChange function or something?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

(EDIT: This works, but isn't as good as the accepted answer.)

Well, yes, I am in fact missing some sort of ApplyChange function.

I don't know how I missed it, but I was looking for this:

EditorUtility.SetDirty(target);

So, in my script, I would just edit the GenerateID function:

public void GenerateID(StateObject s)
{
    s.objID = DateTimeOffset.Now.ToUnixTimeSeconds();
    EditorUtility.SetDirty(s);
}

And I am posting it here in case anyone runs into the same issue, that way they hopefully won't have to spend as much time looking for a solution before being reminded that SetDirty is a thing.

over 4 years ago · Santiago Trujillo Report

0

In my eyes better than mixing in direct accesses from Editor scripts and then manually mark things dirty rather go through SerializedProperty and let the Inspector handle it all (also the marking dirty and saving changes, handle undo/redo etc)

SerializedProperty id;

private void OnEnable()
{
    id = serializedObject.FindProperty("objID");
}

public override void OnInspectorGUI()
{
    DrawDefaultInspector();

    // Loads the actual values into the serialized properties
    // See https://docs.unity3d.com/ScriptReference/SerializedObject.Update.html
    serializedObject.Update();

    if (id.intValue == 0)
    {
        if (GUILayout.Button("Generate Object ID"))
        {
            id.intValue = DateTimeOffset.Now.ToUnixTimeSeconds();
        }
    }

    // Writes back modified properties into the actual class
    // Handles all marking dirty, undo/redo, etc
    // See https://docs.unity3d.com/ScriptReference/SerializedObject.ApplyModifiedProperties.html
    serializedObject.ApplyModifiedProperties();

}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!