question

LetsGoSalmon-6621 avatar image
0 Votes"
LetsGoSalmon-6621 asked JohnTucker-4294 answered

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

I am following a tutorial on YouTube for how to make a Minecraft-like 2D game in Unity. Here's the link: https://www.youtube.com/watch?v=4ubrvkefphI&list=PL37wmdqtgqTIfYtBtAOZNUFR7BeE1XUGi&index=3
I have to scripts to generate the chunks of the world, "GenerateChunk" and "GenerateChunks" (notice the s on the end). This is the 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!

not-supported
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LetsGoSalmon-6621
Thank you for posting your question here, but unfortunately Microsoft Q&A does not support Unity for the time being. I suggest you post your question on the Unity forum.

The list of products currently supported by Microsoft Q&A is as follows: Microsoft Q&A supported products.

0 Votes 0 ·

Thank you! Maybe I can get help there.

0 Votes 0 ·

0 Answers