【问题标题】:Error CS0246 Type or namespace name could not be found when using enum错误 CS0246 使用枚举时找不到类型或命名空间名称
【发布时间】:2020-06-25 18:22:43
【问题描述】:

单例脚本:

 public static ShipSingleton Instance { get { return _instance; } }


    private void Awake()
    {
        if (_instance != null && _instance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            _instance = this;
            DontDestroyOnLoad(this.gameObject);
        }
    }

    public enum Ship
    {
        BasicShip
    };

    public Ship spawnShipID;

生成器对象

public GameObject basicShip;

void Start()
{
    if (ShipSingleton.Instance.spawnShipID == ShipSingleton.Ship.BasicShip)
    {
        Instantiate(basicShip, transform.position, Quaternion.identity);
    }
}

按钮脚本

 public Ship ShipID = ShipSingleton.Ship.BasicShip;

    public void shipchoice()
    {

        SceneManager.LoadScene("watcherqueen");
        ShipSingleton.Instance.spawnShipID = ShipID;

    }

不断收到此错误:

错误 CS0246 找不到类型或命名空间名称“Ship”(您是否缺少 using 指令或程序集引用?

我是否可能在按钮脚本中遗漏了对公共枚举的引用?

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    哦,我知道现在是什么问题了(我也会在另一个问题中解决它)--

    此行需要引用ShipSingleton.Ship 而不仅仅是Ship

    public Ship ShipID = ShipSingleton.Ship.BasicShip;
    

    所以它应该是这样的:

    public ShipSingleton.Ship ShipID = ShipSingleton.Ship.BasicShip;
    

    这是因为枚举类型ShipShipSingleton 的成员。如果Ship 像这样在命名空间级别声明,则没有必要:

    public enum Ship
    {
        BasicShip
    };
    
    public class ShipSingleton
    {
        public static ShipSingleton Instance { get { return _instance; } }
    
    
        private void Awake()
        {
            if (_instance != null && _instance != this)
            {
                Destroy(this.gameObject);
            }
            else
            {
                _instance = this;
                DontDestroyOnLoad(this.gameObject);
            }
        }
    
        Ship spawnShipID;
    }
    

    【讨论】:

    • 完美运行,有些东西我仍然需要解决,但我想我可以自己完成。非常感谢您的宝贵时间。
    • @spagoni 如果对您有用,请不要忘记将答案标记为已接受(答案左侧的绿色勾边复选标记)
    猜你喜欢
    • 2019-07-14
    • 2019-09-14
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多