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.
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
/ ...)
Thanks! That's because these scripts aren't made for Unity at the beginning. I'll change that!
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):
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!
Your answer

Follow this Question
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