【问题标题】:C# Unity the namespace '<global namespace>' already contains a definition forC# Unity 命名空间“<全局命名空间>”已经包含一个定义
【发布时间】:2020-08-19 17:57:37
【问题描述】:

我正在研究访问修饰符,但在我的代码中遇到了以下错误。有人可以向我解释并帮我解决吗? Assets\Testes\Scripts\modificadoracesso.cs(40,7):错误 CS0101:命名空间“”已包含“Felino”的定义

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class modificadoracesso : MonoBehaviour
{

    Felino gatoFase1; // criar objeto
    Felino gatoFase2;
    Filha fi;

    // Start is called before the first frame update
    void Start()
    {
        gatoFase1 = new Felino (); //objeto
        gatoFase2 = new Felino ();
        fi = new Filha();

        //gatoFase1.nome = "mark";
        gatoFase1.ataque();
        gatoFase1.corPelo = "Preto";
        gatoFase1.forca = 100;

        //gatoFase2.nome = "Zuck";
        gatoFase2.corPelo = "Marrom";
        gatoFase2.ataque();

        fi.acessa();

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

class Felino : MonoBehaviour
{

    //Características = atributos
    //protected trabalha dentro a classe ou dentro de uma classe filha
    protected string nome;
    public string corPelo;
    public int forca;

    //Ações = métodos
    public void ataque()
    {
        print("Ataquei");
    }

}

class Filha : Felino
{
    public void acessa()
    {
        nome = "Gato";
    }
}

我已经寻找了一些答案,但到目前为止没有任何效果

【问题讨论】:

  • 全局命名空间部分是因为您的代码中没有命名空间。我不明白为什么你有一个重复的Felino
  • 我是巴西人,我不懂“命名空间”这个句子,它可以用实际的方式告诉我吗?
  • 什么是

标签: c# unity3d


【解决方案1】:

除非一个类在命名空间中,否则它在“全局命名空间”中。在你的类周围添加一个命名空间。我并不是说这是完整的答案,但不使用命名空间是一个坏主意。命名空间通常以您的解决方案名称开头,并在您创建新类时自动放置在那里。

试试这个:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ToDyToScAnO // <-- This is a namespace
{
  public class modificadoracesso : MonoBehaviour
  {

    Felino gatoFase1; // criar objeto
    Felino gatoFase2;
    Filha fi;

    // Start is called before the first frame update
    void Start()
    {
        gatoFase1 = new Felino (); //objeto
        gatoFase2 = new Felino ();
        fi = new Filha();

        //gatoFase1.nome = "mark";
        gatoFase1.ataque();
        gatoFase1.corPelo = "Preto";
        gatoFase1.forca = 100;

        //gatoFase2.nome = "Zuck";
        gatoFase2.corPelo = "Marrom";
        gatoFase2.ataque();

        fi.acessa();

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

class Felino : MonoBehaviour
{

    //Características = atributos
    //protected trabalha dentro a classe ou dentro de uma classe filha
    protected string nome;
    public string corPelo;
    public int forca;

    //Ações = métodos
    public void ataque()
    {
        print("Ataquei");
    }

}

 class Filha : Felino
 {
    public void acessa()
    {
        nome = "Gato";
    }
 }
}

【讨论】:

    【解决方案2】:

    这只是因为您的项目中有另一个具有此名称的脚本! 也许你不小心复制了它什么的。

    【讨论】:

      猜你喜欢
      • 2013-02-22
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-18
      • 2012-08-06
      • 2022-08-13
      相关资源
      最近更新 更多