I recently begun using Editor Extensions for doing simple things during Edit Mode which I’d otherwise would have to do during runtime. For this example, I want to assign a random number to a variable “blubb” on an object’s script “ObjectData” from the editor during Edit Mode:
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System.Collections;
[CustomEditor(typeof(ObjectData))]
public class ObjectEditorExtension : Editor {
#if UNITY_EDITOR
override public void OnInspectorGUI () {
ObjectData objectData = (ObjectData)target;
if(GUILayout.Button("Assign blubb")) {
objectData.blubb = Random.Range(0,10);
}
DrawDefaultInspector();
}
#endif
}
The ObjectData script (just for reference)
using UnityEngine;
using System.Collections;
[System.Serializable]
public class ObjectData : MonoBehaviour {
public int blubb;
}
So far, so good, I click on the button, and a random number is assigned as it should. Once I hit the play button, however, the variable blubb reverts back to it’s default value of 0 (the dwefault value for int variables). Why?