Question with changing script component variable from self-defined editor window...

Hello, everyone.

Here is the problem I met:

I wrote a editor window (which extends from EditorWindow class), which can change variables of a specified script component.

When the script component is attached to a game object which do not link with a prefab, it works fine. But when the game object have a prefab in Asset Folder, it will reset to the prefab state every few moment or when I start to play the game.

Since when I change variable of same game object from inspector works fine, I guess the reason of this problem might because Unity3D do not receive such changing I did from the editor window when the game object have linking with a prefab.

I already found a solution but am not sure if this is the best solution. Any input would be great.

Below is part of my code of editor window:

    function OnGUI () {
      //Check Object
        if(Selection.gameObjects.length > 1 || Selection.gameObjects.length <= 0)
        {
            return;
        }
           
      //Check Component
        var current_obj :  GameObject = Selection.gameObjects[0];
        var ai_simple : AI_Simple = current_obj.GetComponent(AI_Simple);
        if( !ai_simple )
        {
            return;
        }

//Chang Variable example
   if(GUI.Button(rect, "AddnewAI-Mode"))
  {
 ai_simple.AddMode(); //Add a new element into the array
  }

And to solve the problem, I added these codes after “Check Component” part:

        if(PrefabUtility.GetPrefabType(current_obj) == PrefabType.PrefabInstance)
        {
            PrefabUtility.DisconnectPrefabInstance(current_obj);
        }

It can still recover prefab state by pressing “Revert” button, I’m not sure this is a good solution or not , please help me, thank you.

Did you try something like this, to replace the prefab with your new one from hierarchy?

Thank you for reply, asd234w4r5.
No, I didn’t, because I want to keep the gameObject I mentioned can recover to default state of it’s prefab.