C# Unity the namespace '<global namespace>' already contains a definition for

别说谁变了你拦得住时间么 提交于 2021-02-05 09:44:38

问题


I am studying access modifiers and I came across the following error in my code. Can someone explain to me and help me solve it? Assets\Testes\Scripts\modificadoracesso.cs(40,7): error CS0101: The namespace '< global namespace >' already contains a definition for '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";
    }
}

I've looked for some answers but so far nothing works


回答1:


Unless a class is in a namespace, the class it in the 'global namespace'. Add a namespace around your classes. I'm not saying this is the complete answer, but not using namespaces is a bad idea. Namespaces usually start with the name of your solution and will be placed there automatically when you create a new class.

Try this:

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


来源:https://stackoverflow.com/questions/63492526/c-sharp-unity-the-namespace-global-namespace-already-contains-a-definition-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!