How to set non-playmode instantiated objects to dirty?

Currently, I am using Odin Inspector to make quick and dirty editor scripts for a prototype, Right now, I am just using it to make inspector-buttons to run code outside of play mode from a monobehaviour in the scene.

I have it set to instantiate game objects, and then copy over values to those gameobjects when pressed, and it works as expected until I enter play mode, and all of the copied-over values disappear. From what I understand, I need to be marking the objects as dirty but I don’t really understand what method I should be using in my specific case.

The relevant code:

        [Button("Initialize Grid")]
        private void ManualInitializeGrid()
        {
            int x = 0;
            int y = 0;
 
            for (x = tilemap.cellBounds.min.x; x < tilemap.cellBounds.max.x; x++)
            {
                for (y = tilemap.cellBounds.min.y; y < tilemap.cellBounds.max.y; y++)
                {
                    TilemapGridTile tilemapTile = tilemap.GetTile(new Vector3Int(x, y, 0)) as TilemapGridTile;
                    Vector3 position = grid.GetCellCenterWorld(new Vector3Int(x, y, 0));

                    GridTile ob = Instantiate(gridTileObject, tilemap.transform);
                    ob.Initialize(tilemapTile.Info, new Vector2Int(x,y)); //copies over the data

                    ob.transform.position = tilemap.WorldToLocal(position);

                    gridTiles.Add(ob);
                    EditorUtility.SetDirty(ob);
                }
            }
        }

Any help is appreciated!

Odin’s button’s should be dirtying the scene by default. Do you see the * next to the scene name in the hierarchy to indicate the scene is dirty?

If not you could try mark the scene dirty: Unity - Scripting API: SceneManagement.EditorSceneManager.MarkSceneDirty

Are the values correctly serialised in the prefabs as well?

Hey, it turns out that I was not serializing the fields correctly. Thank you!

1 Like