使用枚举时找不到错误CS0246类型或名称空间名称

使用枚举时找不到错误CS0246类型或名称空间名称

本文介绍了使用枚举时找不到错误CS0246类型或名称空间名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单个脚本:

 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;

Spawner对象

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;

    }

继续收到此错误:

按钮脚本中是否可能缺少对公共枚举的引用?

Is it possible I am missing a reference to the public enum in the button script?

推荐答案

哦,我明白了现在的问题(我也会在另一个问题中解决它)-

Oh, I see what the problem is now (and I'll fix it in the other question too) --

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

This line needs to reference ShipSingleton.Ship instead of just Ship:

public Ship ShipID = ShipSingleton.Ship.BasicShip;

所以它应该像这样:

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

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

This is because the enum type Ship is a member of ShipSingleton. It would not be necessary if Ship were declared at the namespace level like this:

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;
}

这篇关于使用枚举时找不到错误CS0246类型或名称空间名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:24