Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Michael_Effraimidis · Jun 08, 2019 at 07:30 PM · inspectormonobehaviour

Monobehaviour class variables not shown on inspector

Assume I have two classes:

 [Serializable]
 public class A : Monobehaviour
 {
     public int _myInt;
     public B _classB;
 }
 [Serializable]
 public class B : Monobehaviour
 {
     public int _myInt;
     public string _myString;
 }

Is there an easy way to make class B variables appear on the inspector on the script of class A?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SirPaddow · Jun 08, 2019 at 08:20 PM

No, and there shouldn't be a way, since MonoBehaviours shouldn't exist if not bound to a gameObject (same reason why you shouldn't do new B();).

But maybe you don't need it to be a MonoBehaviour? If you remove the inheritance, attributes of B will appear in the inspector of A.

Comment
Add comment · Show 4 · 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 Michael_Effraimidis · Jun 08, 2019 at 08:49 PM 0
Share

Unfortunately I use updates so they have to derive from monobehaviour.

avatar image SirPaddow Michael_Effraimidis · Jun 08, 2019 at 08:59 PM 0
Share

Well, it's difficult to understand what you want your code to do, but don't forget you can always do somthing like that:

 [Serializable]
 public class A : $$anonymous$$onoBehaviour
 {
     public int _myInt;
     public B _classB;
 
     public void Update()
     {
         _classB.Update();
     }
 }
 
 [Serializable]
 public class B
 {
     public int _myInt;
     public string _myString;
 
     public void Update()
     {
         // do stuff
     }
 }
avatar image Michael_Effraimidis SirPaddow · Jun 08, 2019 at 09:19 PM 0
Share

I said in the original post the class has to derive from monobehaviour ,because it uses methods that derive from monobehaviour like update and start. And isn't this a very bad practise? To update an object from another one?

Show more comments
avatar image
0

Answer by itaielidan · Jun 08, 2019 at 09:34 PM

The fastest way is to do something like this:

 [Serializable]
  public class A : MonoBehaviour
  {
      public int _myInt;
      public B _classB;
      private void Start(){
              _classB = GameObject.Find("gameObject name that has class b attached").GetCompoment<B>();
       }
      public void Update()
      {
          _classB.Update();
      }
  }
  
  [Serializable]
  public class B
  {
      public int _myInt;
      public string _myString;
  
      public void Update()
      {
          // do stuff
      }
  }
Comment
Add comment · Show 1 · 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 SirPaddow · Jun 08, 2019 at 09:38 PM 0
Share

No, it will throw an exception. If B is no longer a $$anonymous$$onoBehaviour, you can't use it in "GetComponent()"

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

111 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Large serializable field in a MonoBehaviour causing poor performance in Inspector 1 Answer

How can I reference a specific script inside a GameObject from another MonoBehaviour. 2 Answers

Which function is called when i selected something on the Inspector? 0 Answers

Add a button in the component inspector 2 Answers

Binding component with MonoBehaviour so they are added together by inspector's "Add Component". 1 Answer


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