Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bawenang · Jul 31, 2015 at 07:09 AM · custom editorcustom-inspectorcustom inspectorcustom-editor

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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!
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Em3rgency · Oct 01, 2020 at 03:09 PM 0
Share

Old or not, but it works! Thank you for taking the time to respond to this.

avatar image cv_dg Em3rgency · Oct 01, 2020 at 04:58 PM 0
Share

$$anonymous$$y pleasure, glad someone found it useful!

avatar image unity_1dAdndjER3WPsA · Oct 17, 2020 at 01:36 PM 0
Share

why doesn't it work for me? it says that I can't cast unityengine.object to $$anonymous$$yClass... but why??

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges