Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 rick · Nov 13, 2011 at 09:40 AM · classmonobehaviour

How do I use "helper classes" in C# and have them show up in the Inspector?

I may have the terminology wrong, so I apologise, but here's an example of what I mean.

In javascript I discovered you can do this:

 class Foo {    
     var whatsit = 0;    
 }
 
 class Bar {    
     var thingus = "two";    
 }
 
 var stuff = "nonsense";
 var foo:Foo;    
 var bar:Bar;

If you attach that to an object and look at it in the inspector, you'll see the script, and under it you'll have your Stuff variable with "nonsense" in it. Then there will be a collapsible arrow for Foo and Bar with editable whatsit and dingus elements inside them.

I'm trying to do the same thing in C# but seem to be confusing myself on how this structure works. I seem to recall reading that every object in JavaScript inherits from MonoBehaviour, so I've tried to make my side classes do that as well, but while they show up in the inspector they don't have anything in them.

 using UnityEngine;
 using System.Collections;
 
 public class Test2: MonoBehaviour {
     public string stuff = "nonsense";
     public Foo foo;
     public Bar bar;
 
     void Awake(){
         foo = gameObject.AddComponent<Foo>();
         bar = gameObject.AddComponent<Bar>();
     }
 }
 
 public class Foo: MonoBehaviour {
     public int whatsit = 0;
 }
 
 public class Bar: MonoBehaviour{
     public string thingus = "two";
 }

This results in my script showing up on the game object as before, but the Foo and Bar elements are set to "none (Foo)" and "none (Bar)" respectively. I tried doing an "AddComponent" in Awake and Start, but those are obviously not being called in the editor, only during runtime. trying to assign it when I declare the members doesn't work because they're not static members so GameObject.AddComponent won't work.

Can anyone shed some light on this?

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
3
Best Answer

Answer by syclamoth · Nov 13, 2011 at 09:47 AM

The problem here is that you are treating these classes as MonoBehaviours- they really don't need to be that, there's a much simpler way of managing it (which, btw, is basically how the JS version worked).

 public class Test2: MonoBehaviour {
     public string stuff = "nonsense";
     public Foo foo;
     public Bar bar;
 }

 [System.Serializable]
 public class Foo {
     public int whatsit = 0;
 }

 [System.Serializable]
 public class Bar {
     public string thingus = "two";
 }

The [System.Serializable] tag is what tells it to make those little dropdown menus. If they derive from MonoBehaviour, it'll assume that you want to treat them as references to components on other scripts (obviously not what you want to do here).

Comment
Add comment · Show 2 · 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 rick · Nov 13, 2011 at 09:57 AM 0
Share

Aha! That is exactly what I wanted, thank you. I didn't like having the $$anonymous$$onoBehaviour extension on there, but it obviously hadn't even tried to show up without it. Thanks for the quick answer!

avatar image syclamoth · Nov 13, 2011 at 10:01 AM 0
Share

Hot off the moderation queue!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C# Conception - Hide inherited members and functions 1 Answer

Inherited class, trying to overwrite variable. 0 Answers

Do you tend to have scripts to mark prefabs 1 Answer

script needs to derive from monobehaviour 1 Answer

Custom class with coroutine can't be added using the "new" keyword 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