I’m trying to make a 2d tower defense and it looks like this, even though my code is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapGenerator : MonoBehaviour
{
public GameObject Maptile;
[SerializeField] private int mapWidth;
[SerializeField] private int mapHeight;
private List MapTiles = new List();
private List Pathtiles = new List();
private bool ReachedX = false;
private bool ReachedY = false;
private GameObject currentTile;
private int currentIndex;
private int nextIndex;
public Color Pathcolor;
private void Start()
{
generateMap();
}
private List gettopEdgeTiles()
{
List edgeTiles = new List();
for (int i = mapWidth * (mapHeight-1); i < mapWidth * mapHeight; i++)
{
edgeTiles.Add(MapTiles*);*
}
return edgeTiles;
private List getBottomEdgeTiles()
{
List edgeTiles = new List();
for (int i = 0; i < mapWidth; i++)
{
edgeTiles.Add(MapTiles*);*
}
return edgeTiles;
}
private void moveDown()
{
Pathtiles.Add(currentTile);
currentIndex = MapTiles.indexof(currentTile);
nextIndex = currentIndex-mapWidth;
currentTile = MapTiles[nextIndex];
}
private void moveLeft()
{
Pathtiles.Add(currentTile);
currentIndex = MapTiles.indexof(currentTile);
nextIndex = currentIndex-1;
currentTile = MapTiles[nextIndex];
}
private void moveRight()
{
Pathtiles.Add(currentTile);
currentIndex = MapTiles.indexof(currentTile);
nextIndex = currentIndex+1;
currentTile = MapTiles[nextIndex];
}
private void generateMap()
{
for (int y = 0; y < mapHeight; y++)
{
for (int x = 0; x < mapWidth; x++)
{
GameObject newTile = Instantiate(Maptile);
MapTiles.Add(newTile);
newTile.transform.position = new Vector2(x, y);
}
}
List topEdgeTiles = gettopEdgeTiles();
List BottomEdgeTiles = getBottomEdgeTiles();
GameObject StartTile;
GameObject EndTile;
int rand1 = Random.Range(0, mapWidth);
int rand2 = Random.Range(0, mapWidth);
StartTile = topEdgeTiles[rand1];
EndTile = BottomEdgeTiles[rand2];
currentTile = StartTile;
moveDown();
int loopcount = 0;
while (ReachedX == false) // ReachedX == false is the same as !ReachedX
{
loopcount++;
if (loopcount > 100)
{
debug.log(“Loop ran too long! Broke out of it!”);
break;
}
if (currentTile.transform.position.x > EndTile.transform.position.x)
{
moveLeft();
}
else if (currentTile.transform.position.x < EndTile.transform.position.x)
{
moveRight();
}
else
{
ReachedX = true;
}
}
while (ReachedY == false)
{
if (currentTile.transform.position.y > EndTile.transform.position.y)
{
moveDown();
}
else
{
ReachedY = true;
}
}
Pathtiles.Add(EndTile);
foreach(GameObject obj in Pathtiles)
{
obj.GetComponent().color = Pathcolor
}
}
}
someone please help me