Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

318
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda