【问题标题】:Unity c# 3D error CS0116: A namespace cannot directly contain members as fields or methods [duplicate]Unity c# 3D错误CS0116:命名空间不能直接包含成员作为字段或方法[重复]
【发布时间】:2021-03-27 01:28:47
【问题描述】:

我遇到了这个错误,需要帮助,它在 (92,28),但错误仍然存​​在,任何帮助将不胜感激,我 相对 对 c# 编程比较陌生,所以我'我还不完全熟悉编程语言,所以这可能是一个初学者的问题,但即便如此,我总是很感激我做错了什么的答案和解释,以便我可以改进自己。

using UnityEngine;

[CreateAssetMenu(fileName = "Pokemon", menuName = "Pokemon/Create New Pokemon")]

public class PokemonBase : ScriptableObject
{
    [SerializeField] string name;

    [TextArea]
    [SerializeField] string description;

    [SerializeField] Sprite frontSprite;
    [SerializeField] Sprite backSprite;

    [SerializeField] PokemonType type1;
    [SerializeField] PokemonType type2;

    //Base Stats
    [SerializeField] int maxHp;
    [SerializeField] int attack;
    [SerializeField] int defense;
    [SerializeField] int spAttack;
    [SerializeField] int spDefense;
    [SerializeField] int speed;

    [SerializeField] List<LearnableMove> learnableMoves;


    public string Name
    {
        get { return name; }
    }

    public string Description
    {
        get { return description; }
    }

    public Sprite FrontSprite
    {
        get { return frontSprite; }
    }

    public Sprite BackSprite
    {
        get { return backSprite; }
    }

    public PokemonType Type1
    {
        get { return type1; }
    }

    public PokemonType Type2
    {
        get { return type2; }
    }

    public int MaxHp
    {
        get { return maxHp; }
    }

    public int Attack
    {
        get { return attack; }
    }

    public int Defense
    {
        get { return defense; }
    }

    public int SpAttack
    {
        get { return spAttack; }
    }

    public int SpDefense
    {
        get { return spDefense; }
    }

    public int Speed
    {
        get { return speed; }
    }

}

public List<LearnableMove> LearnableMoves 
{
    get { return learnableMoves; }
}

[System.Serializable]

public class LearnableMove
{
    [SerializeField] MoveBase moveBase;
    [SerializeField] int level;

    public MoveBase Base
    {
        get { return moveBase; }
    }

    public int Level
    {
        get { return level; }
    }
}

public enum PokemonType
{
    None,
    Normal,
    Fire,
    Water,
    Grass,
    Electric,
    Ice,
    Fighting,
    Poison,
    Ground,
    Flying,
    Physic,
    Bug,
    Rock,
    Ghost,
    Dragon
} ``` 

【问题讨论】:

  • LearnableMoves 似乎不在一个类中。一定是的。
  • 通常在 C# 中,您需要在命名空间中包含所有类和其他定义。对于扩展某些 Unity 基础的定义,例如 MonoBehaviours 和 ScriptableObjects,情况并非如此。您应该将除PokemonBase 之外的所有内容放在命名空间内的另一个文件中。参见示例用法。 docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
  • 请注意using 关键字的工作原理。它在一个命名空间中包含所有可访问的类、结构...等,因此您可以在另一个文件中使用它。另外,我不确定您是否可以将其保存在同一个文件中。我从未尝试过,但我通常建议您将类和结构保存在单独的文件中。
  • 可能是一个TYPO:你的班级在LearnableMoves之前被}提前关闭

标签: c# unity3d


【解决方案1】:

SpeedLearnableMoves 之间移动右大括号,使后者包含在类中

    public int Speed
    {
        get { return speed; }
    }

}

public List<LearnableMove> LearnableMoves 
{
    get { return learnableMoves; }
}

    public int Speed
    {
        get { return speed; }
    }

    public List<LearnableMove> LearnableMoves 
    {
        get { return learnableMoves; }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多