最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Mar 3 at 19:20 Alejandro VelasquezAlejandro Velasquez 111 silver badge2 bronze badges 1
  • Why is everything static? – derHugo Commented Mar 4 at 8:25
Add a comment  | 

1 Answer 1

Reset to default 3

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.

发布评论

评论列表(0)

  1. 暂无评论