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 /
  • Help Room /
avatar image
0
Question by Thanor23 · Nov 25, 2019 at 12:28 PM · inspectorpropertiesclass object

Show in inspector other class properties

Hi,

First of all, sorry if similar topics exists, but I did not find what I need. I think my case is more specific.

I'm creating a simple RPG and i'm stuck with an issue. I want to have properties in the inspector but not from the main class, i want properties from another class wich is used as a variable in the main class.

Example:

 using UnityEngine;
 public class Character : MonoBehaviour
 {
     #region VARIABLES MEMBRES
     private string _name;
     private string _class;
     private Bar _health;//HERE
     public enum EnumType { Ally, Neutral, Ennemy };
     public EnumType characterType;
 
     #endregion
 
     #region PROPRIETES
 
     public string Name
     {
         get { return _name; }
         set { _name = value; }
     }
     public string Class
     {
         get { return _class; }
         set { _class = value; }
     }
     public Bar Health//HERE
     {
         get { return _health; }
         set { _health = value; }
     }
 
     #endregion
 
     #region CONSTRUCTEUR
 
     public Character(string n)
     {
         Name = n;
         Class = "Human";
         Health = new Bar();
         characterType = EnumType.Neutral;
     }
 
     #endregion
 }
 

Here you can see that i use a property "Health" for my _health variable, which is a "Bar".

Here is the Bar Class:

 using UnityEngine;
 
 public class Bar : MonoBehaviour
 {
     #region VARIABLES MEMBRES
 
     private int _maxValue;//HERE
     private int _currentValue;
 
     #endregion
 
     #region PROPRIETES
 
     public int MaxValue//HERE
     {
         get { return _maxValue; }
         set { _maxValue = value; }
     }
 
     public int CurrentValue
     {
         get { return _currentValue; }
         set { _currentValue = value; }
     }
 
     #endregion
 
     #region CONSTRUCTEUR
 
     public Bar()
     {
         MaxValue = 100;
         CurrentValue = MaxValue;
     }
     public Bar(int v)
     {
         MaxValue = v;
         CurrentValue = MaxValue;
     }
 
     #endregion
 }
 

And I want, when i drag my Character script on a GameObject, that my "MaxValue" variable from my "Bar" Class is shown in the inspector.

I have no idea how to do it, i tried so many times last night and I searched a lot online but i found nothing usefull... I really hope you can help me. And I hope I'm understandable.

Thanks for your time folks!

edit: here is a screen shot of what I have (Character) and what I want (Character Test), hope it is usefull. alt text

unityinspector.png (13.2 kB)
Comment
Add comment · Show 2
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 Hellium · Nov 25, 2019 at 12:31 PM 1
Share

Side note about your current code: your two classes are inheriting from $$anonymous$$onoBehaviour but you have constructors. This is not allowed in Unity.


Either keep your constructor but remove the inheritance (you won't be able to attach the script to a gameObject anymore) or remove the constructor, replace the constructors by a Setup function (for instance) and retrieve the components somehow (direct reference in inspector / call to GetComponent / ...)

avatar image Thanor23 Hellium · Nov 25, 2019 at 01:32 PM 0
Share

Thanks! That's because these scripts aren't made for Unity at the beginning. I'll change that!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Thanor23 · Nov 25, 2019 at 02:12 PM

Ok I apparently just found the solution. I didn't tested it yet, but it appear to work in the inspector. We just need to add [Serializable] to the class and [SerializeField] to its variables.

 [Serializable]
 public class Bar{
     [SerializeField] private int _maxValue;
     [SerializeField] private int _currentValue;
 
     public int MaxValue
     {
         get { return _maxValue; }
         set { _maxValue = value; }
     }
     public int CurrentValue
     {
         get { return _currentValue; }
         set { _currentValue = value; }
     }
 }


Then add again [SerializeField] but in the variable declaration in the class Character:

 using UnityEngine;
 public class Character : MonoBehaviour
 {
     [SerializeField] private Bar _health;
 
 
     public Bar Health
     {
         get { return _health; }
         set { _health = value; }
     }
 }

And here is how it shows in the inspector (with others variables i use): alt text

I really hope it will help someone. Sorry for the self-solving forum. Thanks again to you @Hellium for your correction. Have a nice day folks!


unityinspector.png (15.7 kB)
Comment
Add comment · 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

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

191 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 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

Why are getters getting called more than they should be called 0 Answers

How do I see what Reference properties and Value properties I can address in GetComponent in C#? 0 Answers

Preprocessor conditional code for cleaner inspector options 1 Answer

Creating game objects from inspector 0 Answers

How do I bind the value of a public variable from the inspector to some other variable/constant? 0 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