Scripts don't show in unity inspector/editor
//Player script
using UnityEngine;
public class Player : MonoBehaviour {
[SerializeField]
private Stat health;}
//Stat
using UnityEngine;
[SerializeField] public class Stat { [SerializeField] private BarScript bar;
[SerializeField]
private float maxVal;
[SerializeField]
private float currentVal;
public float CurrentVal
{
get
{
return currentVal;
}
set
{
this.currentVal = value;
bar.Value = currentVal;
}
}
public float MaxVal
{
get
{
return maxVal;
}
set
{
this.maxVal = value;
bar.MaxValue = maxVal;
}
}
}
,using UnityEngine;
[SerializeField] public class Stat { [SerializeField] private BarScript bar;
[SerializeField]
private float maxVal;
[SerializeField]
private float currentVal;
public float CurrentVal
{
get
{
return currentVal;
}
set
{
this.currentVal = value;
bar.Value = currentVal;
}
}
public float MaxVal
{
get
{
return maxVal;
}
set
{
this.maxVal = value;
bar.MaxValue = maxVal;
}
}
}
Answer by Absent_TK421 · Feb 08, 2019 at 07:35 PM
This is a bit of a guess because I cannot tell from your question which of the classes you provided wont show, or whether the last two are duplicates (they look the same to me) but here are some options:
The Issue: Classes that don't inherit from MonoBehaviour will not have inspectors generated by default. In order to attach a script to a game object through the inspector it needs to inherit from MonoBehaviour. So your Stat Class will not be a class you can place on a gameobject directly in this state. In addition, the [SerializeField] tag is not intended to be used in this way. If you do want to serialize tag a class or struct its often better to use [System.Serializable]
Possible Fixes: Make your Stats class a class which inherits from MonoBehaviour, allowing you to drop it on a game object and assign it in the editor. In this case, your class does not need to be serializable, only your fields within the class.
Alternatively, you could change your Stat class to be System.Serializable, then it will have an inspector group generated as a dropdown from your player class.
//Option 1 (Mono Behaviour drag-and-drop)
public class Stat : MonoBehaviour {
//YOUR CODE HERE
}
//Option 2 (Serialize, cannot drag and drop but will appear in player inspector )
[System.Serializable]
public class Stat {
//YOUR CODE HERE
}
Hope this helps.
Your answer
Follow this Question
Related Questions
what is wrong with my C# code? 1 Answer
Some questions about 2d games in unity 2 Answers
What am I doing wrong? 0 Answers
(DISSOLVED) Error : Failed to build apk. (Unity 2017.2.0f3) 2 Answers
Inspector assigned variables not assigning in build 0 Answers