Problem with EditorScripts after Runtime SOLVED

So I’ve been struggling for a few hours now, with a strange thing, so let me explain you the problem.
I have two scripts. One script have one method that I want to call it in the edit mode, so I built the EditorScript and added the Run button which calls the function. In edit mode everything works fine, but when I run the game, some of the modifications that the script did changed back.

Here is a photo with my problem
In the first picture I have the GameObject before running the script
In the second picture is what I get after running the script
In the third is what I get after playing the game.

Here is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using PixelCrushers.DialogueSystem;

public class ActionButton : MonoBehaviour {

    public Sprite buttonSprite;

    private Sprite emptySprite;


    public void Run(){

        emptySprite = Resources.Load<Sprite>("Empty");

        string tName = buttonSprite.name;

        string sufix = tName.Remove(0,tName.IndexOf("_!_")+3);

        int sufixSepIndex = sufix.LastIndexOf('_');

        int widthStart = sufix.IndexOf('w')+1;
        int widthEnd = sufix.IndexOf('h',widthStart);
        float width = float.Parse(sufix.Substring(widthStart, widthEnd-widthStart));

        print(width);

        int heightStart = sufix.IndexOf('h')+1;
        int heightEnd = sufix.IndexOf('_',heightStart);
        float height = float.Parse(sufix.Substring(heightStart,heightEnd-heightStart));

        print(height);

        int xStart = sufix.IndexOf('x')+1;
        int xEnd = sufix.IndexOf('y',xStart);
        float x = float.Parse(sufix.Substring(xStart,xEnd - xStart));

        int yStart = sufix.IndexOf('y')+1;
        float y = float.Parse(sufix.Substring(yStart));

        print(string.Concat("width :",width,"___ height :",height, "\nx :", x, "___ y :", y));

        //position

        RectTransform rt = gameObject.GetComponent<RectTransform>();
        rt.anchoredPosition = new Vector3(x,-y,0);
        rt.anchorMin = new Vector2(0, 1);
        rt.anchorMax = new Vector2(0, 1);
        rt.sizeDelta = new Vector2(width,height);

        //change image
        gameObject.GetComponent<Image>().sprite = emptySprite;

        //set button
        Button btn = gameObject.GetComponent<Button>();
        btn.transition = Selectable.Transition.SpriteSwap;

        SpriteState ss = new SpriteState();
        ss.highlightedSprite = buttonSprite;
        ss.pressedSprite = buttonSprite;
  
        btn.spriteState = ss;

        //change name
        string conversationName = gameObject.GetComponent<ConversationTrigger>().conversation;
        gameObject.name = conversationName;
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(ActionButton))]
public class ActionButtonEditor : Editor {

    public override void OnInspectorGUI(){
      
        DrawDefaultInspector();

        ActionButton actionButton = (ActionButton)target;

        GUILayout.Space(10);

        if(GUILayout.Button("Run")){
            actionButton.Run();
        }
    }

}

Thank you!

if(GUILayout.Button("Run")){
    Undo.RecordObject(actionButton);
    actionButton.Run();
}

Unity doesn’t automatically understand that objects have been changed, and need to be saved to disk. One of the easiest ways to tell Unity to do that is to use Undo.RecordObject. As an added bonus, you now support Undo!

1 Like

I made those changes, but the problem persists.

if(GUILayout.Button("Run")){
            Undo.RecordObject(actionButton,"Change the button attributes");
            actionButton.Run();
        }

Ah, right - you need to make sure that everything that you’re editing is getting marked as dirty. Didn’t read your code properly - you’re editing some other components than the button itself.

I believe that calling Undo.RecordObject on the button’s gameObject should do the trick, as all the other components seem to be attached to that as well.

Thats not going to work cause as soon as you return from Play-mode changes to scene-specific data are lost. you need to apply the changes to an asset (or serialize the changes externally, either to file or server) if you want them saved at run-time.

OP writes that “In edit mode everything works fine, but when I run the game, some of the modifications that the script did changed back”, so I don’t think they’re trying to have Play-mode changes carry over to edit mode.

And to get the Button GameObject in the Editor script, I should create another field, in my ActionButton Script where I drag&drop the gameObject that holds the button and then it will be something like
Undo.RecordObject(myGameObject); ?
Or there is any smarter way that I don’t know

It’s the gameObject of the button. it’s actionButton.gameObject.

Still the same problem…

Anyone have any ideas?

Try recording all of the objects that you’re changing:

    public override void OnInspectorGUI(){
    
        DrawDefaultInspector();
        ActionButton actionButton = (ActionButton)target;
        GUILayout.Space(10);
        if(GUILayout.Button("Run")){

            Undo.RecordObjects(new object[] {
                actionButton.gameObject, ,
                actionButton.GetComponent<Image>(),
                actionButton.GetComponent<Button>() },
                "ActionButton.Run");

            actionButton.Run();
        }
    }

Thank you! This solution worked.

Glad I could help!