Search code examples
unity-game-enginevariablesinspector

Public variables inside namespace class arent showing up in the unity inspector


im trying to access the atk, def, hp, spd, and mana variables on the inspector to add stats to a prefab, but they arent showing up on the inspector for some reason when I attach the script to the gameobject. please help

using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine.UIElements;

namespace Stats
{

    public class Enemy : MonoBehaviour
    {
        public static int atk;
        public static int def;
        public static int hp;
        public static int spd;
        public static int mana;
        public static UnityEngine.UI.Image enemyImage;

        public static GameObject[] infectedLvlOne;
        public static GameObject[] infectedLvlTwo;
        public static GameObject[] infectedLvlThree;
        public static GameObject[] infectedLvlFour;
        public static GameObject[] infectedLvlFive;
        public static GameObject[] infectedLvlSix;
        public static GameObject[] infectedLvlSeven;
        public static GameObject[] infectedLvlEight;
        public static GameObject[] infections;

        private static int baseAtk;
        private static int baseDef;
        private static int baseHp;
        private static int baseSpd;
        private static int baseMana;
        private static int lvl;


        static void SetBaseInfectionStats()
        {
            atk = baseAtk * lvl;
            def = baseDef * lvl;
            hp = baseHp * lvl;
            spd = baseSpd * lvl;
            mana = baseMana * lvl;
        }
    }
}

enter image description here

I have tried adding system serializable to the class and field serializable to the variables I need, but even with that it doesnt work. The variables are public so they should appear.


Solution

  • Static variables are not serializable.

    From Docs:

    To use field serialization, you must ensure it:

    Is public, or has a SerializeField attribute
    Is not static
    Is not const
    Is not readonly
    Has a fieldtype that can be serialized.

    Remove the static keyword from the fields and they will be serialized and show in the inspector.