- Home /
 
Getting a target's gameobject in custom editor
Hi' I've got these classes:
A component MyClass
 public MyClass : Monobehaviour
 {
     public Vector3[] positionList;
     private MyData = null;
     
     void OnEnable()
     {
         if (MyData == null)
             MyData = new MyData(this);
     }
 }
 
               And a class for its data:
 [System.Serializable]
 public MyData
 {
     private int intData = 0;
     
     private MyClass ownerClass = null;
     
     public MyData(MyClass myClass)
     {
         ownerClass = myClass;
     }
     
     public int IntData
     {
         get{return intData;}
         set
         {
             intData = value;
             gameObject.transform.position = GetPosition(value);
         }
     }
     
     private Vector3 GetWorldPosition(int value)
     {
         return ownerClass.positionList[value];
     }
 }
 
               And an editor class: [CustomEditor(typeof(MyClass))] public class MyClassEditor : Editor { private SerializedObject myClassSO; private MyClass myClass;
     void OnEnable () {
         myClassSO = new SerializedObject(target);
         myClass = (MyClass)target;
     }
     
     public override void OnInspectorGUI()
     {
         myClassSO.Update();
         
         base.OnInspectorGUI();
             
          myClass.MyData.IntData = EditorGUILayout.IntField("Position type: ",  myClass.MyData.IntData);
         
         if(GUI.changed){
             EditorUtility.SetDirty(target);
             EditorUtility.SetDirty(myClass);
         }
         
         myClassSO.ApplyModifiedProperties();
     }
 }
 
               It's just a simplification of the real class. But what I wanted to do is to have a custom editor that will change a data by using a property of the variable. In its property, I wanted to also change the transform.position of the gameObject, that's why I also set the value of the MyClass object that has / owns this data in the constructor of MyData. This code works when I pressed play. I can change the value of MyData.IntData and the transform.position of the gameObject just fine. However, in the editor mode, it didn't work because the ownerClass instance is null. Unfortunately, I also need it in the editor so I can change it without having to press play to see the result. So how do I do it?
Thanks in advance.
Answer by cv_dg · Oct 23, 2019 at 11:38 PM
Hey, 
 This seems to be an old post but you can get the gameObject of the target by casting it as the class first. 
 [CustomEditor(typeof(MyClass))]
 public class MyClassEditor : UnityEditor.Editor
 {
     private MyClass myClass;
 
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
         myClass = target as MyClass;
         GameObject go = myClass.gameObject; // YAY!
     }
 }
 
              Old or not, but it works! Thank you for taking the time to respond to this.
$$anonymous$$y pleasure, glad someone found it useful!
why doesn't it work for me? it says that I can't cast unityengine.object to $$anonymous$$yClass... but why??
Your answer
 
             Follow this Question
Related Questions
Events in custom inspector 1 Answer
Custom inspector drop down 0 Answers
How to Hide/Show List or Array in the inspector based on a variable? 0 Answers
Generic CustomEditor 0 Answers
Dictionary becomes null unexpectedly when using a custom inspector 2 Answers