Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Variables changed in EditorWindow script, reverts itself when in playmode

Discussion in 'Scripting' started by Wojzax, Jan 27, 2022.

  1. Wojzax

    Wojzax

    Joined:
    Jul 22, 2014
    Posts:
    34
    This is only part of code from my custom Editor script

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class CharacterEdit : EditorWindow
    5. {
    6.     bool groupEnabled;
    7.    
    8.     [MenuItem("Window/CharacterEdit")]
    9.     public static void ShowWindow()
    10.     {
    11.         EditorWindow.GetWindow(typeof(CharacterEdit));
    12.     }
    13.    
    14.     void OnGUI()
    15.     {
    16.         bool enableEdit=true;
    17.  
    18.         if(enableEdit==true)
    19.         {
    20.              Selection.gameObjects[0].GetComponent<BaseScript>().headObjectIndex=3;
    21.         }
    22.      }
    23. }
    It changes certain variable in selected object's script. Works fine in the editor.
    But when I hit play, the variable reverts itself back to "inspector value" of 0. How to save this in the scene Inspector permanently?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. Wojzax

    Wojzax

    Joined:
    Jul 22, 2014
    Posts:
    34
    I've added this two lines above variable changing, yet when I press Play variable once again is 0. No other script is making this variable go to 0.

    And the selected object is a Prefab that have this variable set to 0 in Inspector

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. public class CharacterEdit : EditorWindow
    4. {
    5.     bool groupEnabled;
    6.  
    7.     [MenuItem("Window/CharacterEdit")]
    8.     public static void ShowWindow()
    9.     {
    10.         EditorWindow.GetWindow(typeof(CharacterEdit));
    11.     }
    12.  
    13.     void OnGUI()
    14.     {
    15.         bool enableEdit=true;
    16.         if(enableEdit==true)
    17.         {
    18.             Undo.RecordObject(Selection.gameObjects[0], "variable change");
    19.          
    20.             Selection.gameObjects[0].GetComponent<BaseScript>().headObjectIndex=3;
    21.  
    22.             PrefabUtility.RecordPrefabInstancePropertyModifications(Selection.gameObjects[0]);
    23.         }
    24.      }
    25. }
     
    Last edited: Jan 28, 2022
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    You're recording the GameObject. It doesn't change in your use case above.

    If line 20 above is the change in question, you need to record changes to the BaseScript instance.

    Also, if you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.
     
  5. Wojzax

    Wojzax

    Joined:
    Jul 22, 2014
    Posts:
    34
    Wow, I didn't realize that an Object could be a Component.

    So yeah, future me, instead of
    Code (CSharp):
    1. Undo.RecordObject(gameObject, "variable change");
    use
    Code (CSharp):
    1. Undo.RecordObject(gameObject.GetComponent<script>(), "variable change");
    Thanks a lot :)