Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Error: The namespace '<global namepspace>' already contains a definition for 'GenerateChunk'

Discussion in 'Scripting' started by LetsGoSalmon, Sep 16, 2021.

  1. LetsGoSalmon

    LetsGoSalmon

    Joined:
    Aug 17, 2021
    Posts:
    5
    Hi, I'm following a tutorial on YouTube on how to create a Minecraft-like 2D game, here's the link:


    Right now I'm working on the world generation, but i get this error: see title. Here's the code for the two scripts:

    GenerateChunk:

    using UnityEngine;
    using System.Collections;

    public class GenerateChunk : MonoBehaviour {

    public GameObject DirtTile;
    public GameObject GrassTile;
    public GameObject StoneTile;

    public int width;
    public float heightMultiplier;
    public int heightAddition;

    public float smoothness;

    [HideInInspector]
    public float seed;

    void Start () {
    Generate ();
    seed = Random.Range (-10000, 10000);
    }

    public void Generate () {
    for (int i = 0; i < width; i++){
    int h = Mathf.RoundToInt (Mathf.PerlinNoise (seed, (i + transform.position.x) / smoothness) * heightMultiplier) + heightAddition;
    for (int j = 0; j < h; j++){
    GameObject selectedTile;
    if (j < h - 4) {
    selectedTile = StoneTile;
    } else if (j < h - 1 ){
    selectedTile = DirtTile;
    } else {
    selectedTile = GrassTile;
    }

    GameObject newTile = Instantiate (selectedTile, Vector3.zero, Quaternion.identity) as GameObject;
    newTile.transform.parent = this.GameObject.transform;
    newTile.transform.localPosition = new Vector3 (i, j);
    }
    }
    }
    }


    GenerateChunks:

    using UnityEngine;
    using System.Collections;

    public class GenerateChunks : MonoBehaviour {

    public GameObject chunk;
    int chunkWidth;
    public int numChunks;
    float seed;

    void Start () {
    chunkWidth = chunk.GetComponent<GenerateChunk> ().width;
    seed = Random.Range (-100000f, 100000f);
    Generate ();
    }

    public void Generate () {
    int lastX = -chunkWidth;
    for (int i = 0; i < numChunks; i++) {
    GameObject newChunk = Instantiate(chunk, new Vector3(lastX + chunkWidth, 0f), Quaternion.identity) as GameObject;
    newChunk.GetComponent<GenerateChunk> ().seed = seed;
    lastX += chunkWidth;
    }
    }
    }



    Please help me!
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
  3. UncleanWizard

    UncleanWizard

    Joined:
    Jun 5, 2021
    Posts:
    38
    The class name "GenerateChunk" probably already is taken try renaming the file that causes the error and find the duplicate class/file and remove it.
     
  4. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    761
    Just like the error says, a GenerateChunk already exists in the global namespace. Check whether you accidentally have a duplicate script in the project. And it's best to put your code in its own namespace.
     
  5. LetsGoSalmon

    LetsGoSalmon

    Joined:
    Aug 17, 2021
    Posts:
    5
    This might sound a bit n00by, but explain the last sentence...
     
  6. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    761
    I use always Namespaces, normally the project name.
    This is very useful for managing code and at some point also necessary, because otherwise name conflicts will certainly occur.

    Code (CSharp):
    1. namespace SampleNamespace
    2. {
    3.     class SampleClass
    4.     {
    5.         public void SampleMethod()
    6.         {
    7.             System.Console.WriteLine(
    8.                 "SampleMethod inside SampleNamespace");
    9.         }
    10.     }
    11. }