Variables are not visible in script Inspector window

送分小仙女□ 提交于 2020-12-26 08:12:27

问题


I started Unity a couple of days ago and I'm confused.
This is my current workspace:

This is what is should look like:

I should be able to fill in the variables but I can't.


回答1:


  1. To show properties/variables in the inspector, they must be public, if they are not they won't appear.

  2. You can also view private fields by attributing them as [SerializeField].

  3. To view custom classes in the inspector, you should mark them as [Serializable].

  4. To hide public variables from inspector, use [HideInInspector] or [System.NonSerialized] attribute.

Here is a sample:

public class SomePerson : MonoBehaviour 
{
    //This field gets serialized because it is public.
    public string name = "John";

    //This field does not get serialized because it is private.
    private int age = 40;

    //This field gets serialized even though it is private
    //because it has the SerializeField attribute applied.
    [SerializeField]
    private bool hasHealthPotion = true;

    // This will be displayed in the inspector because the class has Serialized attribute.
    public SomeCustomClass somCustomClassInstance;

    // This will not be shown in inspector even if it is public.
    [HideInInspector]
    public bool hiddenBool;    

    // Same with this one.
    [System.NonSerialized]
    public int nonSerializedVariable = 5;
}

[System.Serializable]
public class SomeCustomClass
{
    public string someProperty;
}


来源:https://stackoverflow.com/questions/39745357/variables-are-not-visible-in-script-inspector-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!