Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED] Variables not showing in Inspector

Discussion in 'Scripting' started by RedAndBlackGames, Mar 19, 2016.

Thread Status:
Not open for further replies.
  1. RedAndBlackGames

    RedAndBlackGames

    Joined:
    Mar 18, 2016
    Posts:
    3
    Hi, I'm not 100% sure if this is the right forum for this, so I'm sorry ahead of time.

    I've been having a problem for a few days, I have a script and I need to access the variables from the inspector. Most of the variables are private with the [SerializeField] attribute and there are a few public ones, but none of them are showing, not even the Script field that directs me to where the script is in my project folders.
    It was fine before, but I'm not sure what's wrong, I've removed it and put it back on, I've restarted Unity, but nothing seems to work.
    I attached a picture if you need a clearer example.

    Any help would be greatly appreciated! <3

    Here's my code if anyone wants to take a look at it, but I'm not sure the problem is in the code because the code still runs fine.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityStandardAssets.Characters.FirstPerson;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7.  
    8. [AddComponentMenu("Characters/Player")]
    9. [System.Serializable]
    10. public class Character : MonoBehaviour
    11. {
    12.     [Header("*BASIC INFO*")]
    13.     public string characterName = " ";
    14.     [SerializeField] private int _credits;
    15.     private const int BASE_VITAL_VALUE = 100;
    16.     private const int BASE_SKILL_VALUE = 10;
    17.  
    18.     [Header("*INVENTORY INFO*")]
    19.     [Space(10)]
    20.     //[SerializeField] private ItemDatabase _itmDatabase;
    21.     [SerializeField] private CharacterScreenUIHandler _csuih;
    22.     [SerializeField] private List<Item> _inventory;
    23.     [SerializeField] private float _weight;
    24.     [SerializeField] private float _curWeight;
    25.  
    26.     [SerializeField]private Weapon leftHand;
    27.     [SerializeField]private Weapon rightHand;
    28.  
    29.     [Header("*LEVEL INFO*")]
    30.     [Space(10)]
    31.     [SerializeField] private int _level = 1;
    32.     [SerializeField] private int _expToLevel;
    33.     [SerializeField] private int _exp = 0;
    34.  
    35.     [Header("*VITAL INFO*")]
    36.     [Space(10)]
    37.     [SerializeField] private float _health = BASE_VITAL_VALUE;
    38.     [SerializeField] private float _curHealth = BASE_VITAL_VALUE;
    39.     [SerializeField] private float _mana = BASE_VITAL_VALUE;
    40.     [SerializeField] private float _curMana = BASE_VITAL_VALUE;
    41.     [SerializeField] private float _stamina = BASE_VITAL_VALUE;
    42.     [SerializeField] private float _curStamina = BASE_VITAL_VALUE;
    43.  
    44.     [Header("*STAT INFO*")]
    45.     [Space(10)]
    46.     [SerializeField] private int _strength = BASE_SKILL_VALUE;
    47.     [SerializeField] private int _agility = BASE_SKILL_VALUE;
    48.     [SerializeField] private int _willpower = BASE_SKILL_VALUE;
    49.     [SerializeField] private int _perception = BASE_SKILL_VALUE;
    50.     [SerializeField] private int _endurance = BASE_SKILL_VALUE;
    51.     [SerializeField] private int _intelligence = BASE_SKILL_VALUE;
    52.     [SerializeField] private int _diplomacy = BASE_SKILL_VALUE;
    53.     [SerializeField] private int _luck = BASE_SKILL_VALUE;
    54.     [SerializeField] private int _statPoints = 0;
    55.  
    56.     [Header("*SKILL INFO*")]
    57.  
    58.     //move these to PlayerUI
    59.     //public Slider healthSlider;
    60.     //public Slider manaSlider;
    61.     //public Slider staminaSlider;
    62.     public FirstPersonController fpsC;
    63.     private CharacterController _ctrl;
    64.  
    65.     [SerializeField] private float _minSurviveTime = 2f;
    66.     [SerializeField] private float _damageForSeconds = 1f;
    67.     private float _airTime = 0f;
    68.  
    69.     private float _fillAmount;
    70.     public Image healthImage;
    71.     public Image manaImage;
    72.     public Image staminaImage;
    73.  
    74.     [SerializeField] private bool _initialized = false;
    75.     [SerializeField] private bool _alive = true;
    76.     [SerializeField] private bool _isPC = false;
    77.  
    78.     private CursorLockMode _lockMode;
    79.  
    80.     #region get/set
    81.     #region inventory
    82.     public List<Item> CInventory { get { return _inventory; } set { _inventory = value; } }
    83.     public int Credits { get { return _credits; } set { _credits = value; } }
    84.     public float Weight { get { return _weight; } set { _weight = value; } }
    85.     public float CurrentWeight { get { return _curWeight; } set { _curWeight = value; } }
    86.     #endregion
    87.     #region level
    88.     public int CharLevel { get { return _level; } set { _level = value; } }
    89.     public int ExpToLevel { get { return _expToLevel; } set { _expToLevel = value; } }
    90.     public int Exp { get { return _exp; } set { _exp = value; } }
    91.     #endregion
    92.     #region vitals
    93.     public float Health { get { return _health; } set { _health = value; } }
    94.     public float Mana { get { return _mana; } set { _mana = value; } }
    95.     public float Stamina { get { return _stamina; } set { _stamina = value; } }
    96.  
    97.     public float CurrentHealth { get { return _curHealth; } set { _curHealth = value; } }
    98.     public float CurrentMana { get { return _curMana; } set { _curMana = value; } }
    99.     public float CurrentStamina { get { return _curStamina; } set { _curStamina = value; } }
    100.     #endregion
    101.     #region skills
    102.     public int Strength { get { return _strength; } set { _strength = value; } }
    103.     public int Agility { get { return _agility; } set { _agility = value; } }
    104.     public int Willpower { get { return _willpower; } set { _willpower = value; } }
    105.     public int Perception { get { return _perception; } set { _perception = value; } }
    106.     public int Endurance { get { return _endurance; } set { _endurance = value; } }
    107.     public int Intelligence { get { return _intelligence; } set { _intelligence = value; } }
    108.     public int Luck { get { return _luck; } set { _luck = value; } }
    109.     public int Diplomacy { get { return _diplomacy; } set { _diplomacy = value; } }
    110.     public int StatPoints { get { return _statPoints; } set { _statPoints = value; } }
    111.     #endregion
    112.  
    113.     #endregion
    114.  
    115.     public void Awake()
    116.     {
    117.         if (_isPC)
    118.         {
    119.             //healthSlider.maxValue = Health;
    120.             //manaSlider.maxValue = Mana;
    121.             //staminaSlider.maxValue = Stamina;
    122.             //healthSlider.value = healthSlider.maxValue;
    123.             //manaSlider.value = manaSlider.maxValue;
    124.             //staminaSlider.value = staminaSlider.maxValue;
    125.             fpsC = GetComponent<FirstPersonController>();
    126.             _lockMode = CursorLockMode.Locked;
    127.  
    128.             //_playerUI = GetComponent<PlayerUI>();
    129.         }
    130.  
    131.         _ctrl = GetComponent<CharacterController>();
    132.  
    133.         if (!_initialized)
    134.             Initialize();
    135.     }
    136.  
    137.  
    138.     #region initialization
    139.     private void Initialize()
    140.     {
    141.         name = characterName;
    142.         if (tag == "Player" || tag == "Compainon")
    143.             CalculateXP();
    144.         CalculateVitals();
    145.         CurrentHealth = Health;
    146.         CurrentMana = Mana;
    147.         CurrentStamina = Stamina;
    148.         //CalculateSkills();
    149.         CalculateInvWeight();
    150.  
    151.  
    152.         if (!_alive)
    153.             _alive = true;
    154.  
    155.         _initialized = true;
    156.     }
    157.  
    158.     public void CalculateVitals()
    159.     {
    160.         int baseHealth = (int)Mathf.Round((Endurance / .10f) + (Strength));
    161.         if (CharLevel == 1)
    162.         {
    163.             Health = baseHealth;
    164.         }
    165.         else
    166.         {
    167.             Health = baseHealth + (int)Mathf.Round(Endurance / .7f);
    168.         }
    169.         Mana = (int)Mathf.Round(((Willpower / 2) + (Willpower / 3)) / .10f);
    170.         Stamina = (int)Mathf.Round((Strength + Endurance + Agility) / .3f);
    171.     }
    172.     public void CalculateSkills()
    173.     {
    174.     }
    175.     public void CalculateInvWeight()
    176.     {
    177.         Weight = Strength * 10;
    178.     }
    179.     #endregion
    180.  
    181.  
    182.     public void LateUpdate()
    183.     {
    184.         if (_isPC)
    185.         {
    186.             //healthSlider.value = CurrentHealth;
    187.             //manaSlider.value = CurrentMana;
    188.             //staminaSlider.value = CurrentStamina;
    189.             UpdateHealthUI();
    190.  
    191.             if (TimeManager.callForPause != 0)
    192.             {
    193.                 _lockMode = CursorLockMode.None; Cursor.visible = true; fpsC.enabled = false;
    194.             }
    195.             else if (TimeManager.callForPause == 0)
    196.             { _lockMode = CursorLockMode.Locked; Cursor.visible = false; fpsC.enabled = true; }
    197.         }
    198.  
    199.         CalculateFalling();
    200.     }
    201.  
    202.     #region methods
    203.     #region inventory
    204.     public void AddCredits(int value){Credits += value;}
    205.     public void RemoveCredits(int value){Credits -= value;}
    206.     public void AddWeight(float w){CurrentWeight += w;}
    207.     public void SubWeight(float w) {CurrentWeight -= w;}
    208.     public void AddCarryweight(float w) { Weight += w; }
    209.     public void SubCarryWeight(float w) { Weight -= w; }
    210.     public void AddItem(Item i, int count)
    211.     {
    212.         if (CInventory.Count == 0)
    213.         {
    214.             CInventory.Add(((Item)i));
    215.             AddWeight(i.Weight);
    216.             i = null;
    217.             return;
    218.         }
    219.         else
    220.         {
    221.             for (int cnt = 0; cnt < CInventory.Count; cnt++)
    222.             {
    223.                 if (CInventory[cnt].Name == i.Name)
    224.                 {
    225.                     CInventory[cnt].Count += count;
    226.                     AddWeight(CInventory[cnt].Weight);
    227.                     //return;
    228.                 }
    229.                 else if (CInventory[cnt].Name != i.Name)
    230.                 {
    231.                     CInventory.Add(((Item)i));
    232.                     AddWeight(i.Weight);
    233.                     string _typ = "";
    234.                     if (i.GetType() == typeof(Item))
    235.                         _typ = "Misc.";
    236.                     else if (i.GetType() == typeof(Weapon))
    237.                         _typ = "Weapon";
    238.                     else if (i.GetType() == typeof(Armor))
    239.                         _typ = "Armor";
    240.                     else if (i.GetType() == typeof(Consumable))
    241.                         _typ = "Consumable";
    242.                     else
    243.                         Debug.LogError("Type Not Recognized!"); _typ = "ERROR";
    244.  
    245.                     _csuih.AddItemSlot(i.Name, i.Weight.ToString(), i.Value.ToString(), i.Count.ToString(), _typ);
    246.                     i = null;
    247.                     return;
    248.                 }
    249.             }
    250.             i = null;
    251.             return;
    252.         }
    253.     }
    254.  
    255.  
    256.     public void RemoveItem(Item i, int a)
    257.     {
    258.         for (int cnt = 0; cnt < a; cnt++)
    259.         {
    260.             CInventory.Remove(i);
    261.             SubWeight(i.Weight);
    262.         }
    263.     }
    264.     #endregion
    265.     #region level
    266.     public void CalculateXP() { ExpToLevel = (int)Mathf.Round(37.5f * Mathf.Pow(CharLevel, 2) + 87.5f * CharLevel - 124); }
    267.     private void AddXp(int exp)
    268.     {
    269.         Exp += exp;
    270.         if (Exp >= ExpToLevel) { LevelUp(); }
    271.     }
    272.     private void LevelUp()
    273.     {
    274.         CharLevel++;
    275.         Exp = 0;
    276.         CalculateXP();
    277.         StatPoints += 5;
    278.     }
    279.     #endregion
    280.     #region vitals
    281.     public void Heal(float h, float m, float s)
    282.     {
    283.         CurrentHealth += h;
    284.         CurrentMana += m;
    285.         CurrentStamina += s;
    286.     }
    287.     public void Damage(float h, float m, float s)
    288.     {
    289.         CurrentHealth -= h;
    290.         CurrentMana -= m;
    291.         CurrentStamina -= s;
    292.         Debug.LogWarning("Took damage!");
    293.     }
    294.  
    295.     public void AddMax(float h, float m, float s)
    296.     {
    297.         Health += h;
    298.         Mana += m;
    299.         Stamina += s;
    300.     }
    301.     public void SubMax(float h, float m, float s)
    302.     {
    303.         Health -= h;
    304.         Mana -= m;
    305.         Stamina -= s;
    306.     }
    307.     public void Death()
    308.     {
    309.         _alive = false;
    310.         //Do death stuffs
    311.     }
    312.     #endregion
    313.     #region stats
    314.     public void AddStat(int s, int a, int w, int p, int e, int i, int l, int d)
    315.     {
    316.         Strength += s;
    317.         Agility += a;
    318.         Willpower += w;
    319.         Perception += p;
    320.         Endurance += e;
    321.         Intelligence += i;
    322.         Luck += l;
    323.         Diplomacy += d;
    324.     }
    325.     public void SubStat(int s, int a, int w, int p, int e, int i, int l, int d)
    326.     {
    327.         Strength -= s;
    328.         Agility -= a;
    329.         Willpower -= w;
    330.         Perception -= p;
    331.         Endurance -= e;
    332.         Intelligence -= i;
    333.         Luck -= l;
    334.         Diplomacy -= d;
    335.     }
    336.     #endregion
    337.  
    338.     #endregion
    339.  
    340.  
    341.     public void UpdateHealthUI()
    342.     {
    343.         //healthSlider.value = CurrentHealth;
    344.         //manaSlider.value = CurrentMana;
    345.         //staminaSlider.value = CurrentStamina;
    346.  
    347.         healthImage.fillAmount = Map(CurrentHealth, 0f, Health, 1f, 0f);
    348.         manaImage.fillAmount = Map(CurrentMana, 0f, Mana, 1f, 0f);
    349.         staminaImage.fillAmount = Map(CurrentStamina, 0f, Stamina, 1f, 0f);
    350.     }
    351.  
    352.     private float Map(float value, float min, float max, float outMax, float outMin)
    353.     {
    354.         return (value - min) * (outMax - outMin) / (max - min) + outMin;
    355.     }
    356.  
    357.     private void CalculateFalling()
    358.     {
    359.         if (!_ctrl.isGrounded)
    360.         {
    361.             _airTime += Time.deltaTime;
    362.         }
    363.         if (_ctrl.isGrounded)
    364.         {
    365.             if (_airTime > _minSurviveTime)
    366.             {
    367.                 Damage(_damageForSeconds * _airTime, 0, (_damageForSeconds * _airTime) / 2);
    368.             }
    369.             _airTime = 0;
    370.         }
    371.     }
    372.  
    373.     public void Deinitialize()
    374.     {
    375.         //Save S*** here
    376.         _initialized = false;
    377.     }
    378. }
    379.  
    380.  
     

    Attached Files:

    Last edited: Mar 19, 2016
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Oh I see your problem, you forgot to "Save S*** here" :)

    Seriously, is it possible you have a compiler error? You can inadvertently flip a button on the Console window that hides all errors (look near the upper right corner for the filters), and then not even know you have them when they are in fact preventing your running and your compilation.

    ALSO, try just picking one C# file and right-click-reimport that one file: it will trigger a full recompile.
     
  3. RedAndBlackGames

    RedAndBlackGames

    Joined:
    Mar 18, 2016
    Posts:
    3
    /facepalm
    I figured it out!
    I had created an (unfinished) editor for that script that didn't display anything, I deleted it and everything showed up again!
    I've had it for a while and it didn't do anything, so I don't really understand why it randomly decided to do something.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Yay! Just be sure to Save S*** here and you should be fine!
     
    SymphonicYT likes this.
  5. techslugz

    techslugz

    Joined:
    Dec 8, 2020
    Posts:
    4
    Im getting the same problem, I did save?? Sorry, noob in advance..

    All my variables are Public.. Haven't used the
    1. [AddComponentMenu("Characters/Player")]
    2. [System.Serializable]
    but surely I shouldnt have to as all my variables are Public no??
     
  6. techslugz

    techslugz

    Joined:
    Dec 8, 2020
    Posts:
    4
    UPDATE - IGNORE ME ALL SORTED LOL I guess it re compiled.... all working now.

    If anybody can explain or point me in the right direction for :
    1. [AddComponentMenu("Characters/Player")]
    2. [System.Serializable]
    would be appreciated. From what I can gather, it is to make private variables show up in the inspector? Am I right?
     
  7. williamqimingshi1648

    williamqimingshi1648

    Joined:
    May 2, 2021
    Posts:
    4
    Here is my script
    sing System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Player : MonoBehaviour
    {
    void Start () {

    }
    void Update () {

    }
    void FixedUpdate () {
    public float speed;
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");
    Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
    rigidbody.AddForce(movement*speed*Time.deltaTime);
    }
    }
    I don't understand why the variable won't show up on the inspector
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,907
    Please open a new thread (and only one!) when you have your question. It is free and it helps to organize questions and answers and makes our life easier.
    Please use code tags if you submit scripts. ALWAYS. https://forum.unity.com/threads/using-code-tags-properly.143875/

    You're missing an 'u' here.

    Since you don't have any more variables in your text here, I'm assuming this is the one you're talking about. You will need to move it out of the method into the class in order to show up in the inspector. Also make it either public or decorate it with the [SerializeField] attribute.
     
    Last edited: May 6, 2021
    Kurt-Dekker likes this.
  9. Doncaster

    Doncaster

    Joined:
    Feb 5, 2021
    Posts:
    1
    re-importing worked for me thanks!
     
    Tinuvia likes this.
  10. PurpledArtFrog

    PurpledArtFrog

    Joined:
    Aug 6, 2018
    Posts:
    4
    Well - I had some variation of this issue. Fields not showing up - eventhough they had the SerializeField decorator. Recompiled all and restarted Unity and Visual Studio... Added public to one of the declarations * poof * ... they all appeared. Now public is removed again - and all fields are still there.
     
  11. Slycodger

    Slycodger

    Joined:
    Nov 23, 2021
    Posts:
    2
    how did you get to 25000 posts, i see you in almost every thread.
     
  12. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    160
    You answered your own question :D
     
    RemDust likes this.
  13. dreg_master

    dreg_master

    Joined:
    Jan 4, 2016
    Posts:
    44
    ..Also, I had this issue, i was using a different CLASS of variables and its data would not show on inspector. With CUSTOM classes you need to make them [system.Serializable] for it to work.
    EG
    Code (CSharp):
    1.  
    2. [SerializeField] private Precept[] AllQuestionsPrecepts;
    3. //Would not show in inspector? Why?
    4.  
    Code (CSharp):
    1.  
    2. [System.Serializable] //Without this it wont show
    3. public class Precept
    4. {
    5.     public int level;
    6.     public string reference;
    7.     public string verse;
    8.     public int weight;
    9. }
    Any other Scripts referencing this class for data would not show up in inspector unless [system.serializable] was on the class.
     
    Last edited: Jul 10, 2022
    abadrangui likes this.
  14. mets_on_unity

    mets_on_unity

    Joined:
    Jul 5, 2023
    Posts:
    1
    CTR+R in unity after you compiled and saved the script!
     
  15. unity_30cmiracle

    unity_30cmiracle

    Joined:
    Oct 18, 2023
    Posts:
    2
    Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ConfettiSpawnScript : MonoBehaviour
    6. {
    7.  
    8.     public GameObject confetti;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         Instantiate(confetti, Random.Range(-30, 30), transform.position.y, transform.rotation);
    20.     }
    21. }
     
  16. unity_30cmiracle

    unity_30cmiracle

    Joined:
    Oct 18, 2023
    Posts:
    2
    Nevermind I was able to fix it
     
  17. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,796
    Locking the thread as it is very old.
     
Thread Status:
Not open for further replies.