- Home /
How to access Custom Inspector own gameObject
I would like to know how can I access the gameObject of my Custom Inspector.
Thanks a lot !
Could you post a screenshot of your custom inspector? Just to understand a bit more what's your problem
I just need to access in the code my gameObject maybe using target or SerializedObject. Should I need to cast it ? Like (GameObject)target ?
Answer by Baste · Jan 09, 2015 at 02:54 PM
If you have written a custom inspector for the script MyScript, the target variable is of the type MyScript. As long as MyScript is a monobehaviour, you can grab it's gameObject as normal, you just need the cast to MyScript first. I like to grab the script in OnEnable, and make it available as a field:
 [CustomEditor(typeof(MyScript))]
 public class MyScriptEditor : Editor {
 
     MyScript script;
     GameObject scriptObject;
 
     void OnEnable() {
         script = (MyScript) target;
         scriptObject = script.gameObject;
     }
 
     void OnInspectorGUI() {
         ...
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
[Editor Scripting] Call Function in other GameObjects Editor Script? 0 Answers
DestroyImmediate(component.gameObject) destroys component but not gameObject 1 Answer
How to add a component on a GameObject in Custom Inspector 1 Answer
What is a GUILayoutOption[] 3 Answers
Is it possible to create a custom gettter/setter on SerializedProperty ? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                