Prefab modified by script in Editor loses changes when IsPlaying

Looked all over, no solution. This is simple. I drag and drop a prefab onto my scene. It’s built in script randomly resizes it. That works fine. But when I run it, the prefab gets resized again.

I control whether or not to run the resize routine with a public boolean, and a private boolean. Basically, when the user checks the box to make it Unique, it runs the resize and sets the private boolean to show that it’s already been done, so that it doesn’t run in every iteration of Update.

Works great in Editor. I can resize and restore to my heart’s content. But when I run it, apparently the value for the private bool is being lost because it’s running the resize routine. And when I stop the run, it runs it again.

I need this to resize once, in the editor, on command, and then remember those settings/scale when it’s run and when the run is stopped. I’m not using a custom editor, but I’ve tried SetDirty, and Serialization. Nothing is working.

Here is most of the code, the part that controls when the resize (Randomize) function is run.
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.Collections;

public class AlienEditor : MonoBehaviour {

    public bool MakeUnique;

    private bool IsUnique; // To determine if we need to change it.
	// Use this for initialization
	void Start () {
            // Not doing anything in here!

	// Update is called once per frame
	void Update () {
        // Don't do this when the game is running, only in the editor
        if (!Application.isPlaying)
            // Make it unique
            if ((MakeUnique) && !(IsUnique))
                Debug.Log("Time to Make Unique!");
                IsUnique = true;

            // Make it standard
            if (!(MakeUnique) && (IsUnique))
                Debug.Log("Time to Load the Prefab!");
                IsUnique = false;
        }// Not Playing

Hi, yes, the private variables are not serialized so it resets when the game runs.

There are other way of doing this in the editor, depending on your needs:

1-Use the OnValidate() callback. It’s called in editor only and only when a value is changed in the inspector. So during this callback, you could test the value of the MakeUnique bool, and if true: randomize, if false, reset.

2-Use a menu extension with option to randomize or reset the selected object (use the Selection editor object to know what object is selected)

3-use a context menu attribute with a randomize & a reset item.

4-Use an editor window so you can design lots of options and feedback to the user

To call something when instantiating in the editor, there is also the OnEnable() callback that is called in editor & in game when the ExecuteInEditMode attribute is used. It might be more convenient than Update().

I hope some of those options will help !
EDIT: there is also the SerializeField attribute to serialize a private variable.

Found the problem. It turned out my custom animations contained keyframes for Scale, and this was overriding the editor scaling.

yes, i had the same issue, the changes made by my editor script are getting stored on nonprefab but for prefabs, it was getting reset at a run time.

Here is the method you need to call to store it PrefabUtility.RecordPrefabInstancePropertyModifications(Gameobject.component)
you need to pass the component (script name) whose variable is changed.