2d Roguelike Tutorial help
I'm having trouble adding the BoardManager and GameManager scripts to the GameManager object. I keep getting errors that the script class can't be found even though I'm certain my code is accurate. I also don't understand my compile errors I'm getting ("Type BoardManager already defines a member called X"). Are these caused by having files with the same name in the Complete-Game folder? Anyone able to offer insight?
using UnityEngine; using System; using System.Collections.Generic; using Random = UnityEngine.Random;
namespace Completed {
public class BoardManager : MonoBehaviour
{
[Serializable]
public class Count
{
public int maximum;
public int minimum;
public Count(int min, int max)
{
maximum = max;
minimum = min;
}
}
public int columns = 8;
public int rows = 8;
public Count wallCount = new Count(5, 9);
public Count foodCount = new Count(1, 5);
public GameObject exit;
public GameObject[] floorTiles;
public GameObject[] wallTiles;
public GameObject[] foodTiles;
public GameObject[] enemyTiles;
public GameObject[] outerWallTiles;
private Transform boardHolder;
private List<Vector3> gridPositions = new List<Vector3>();
void IntialiseList()
{
gridPositions.Clear;
for (int x = 1; x < columns - 1; x++)
{
for (int y = 1; y < rows - 1; y++)
{
gridPositions.Add(new Vector3(x, y, 0f));
}
}
}
void BoardSetup()
{
boardHolder = new GameObject("Board").transform;
for (int x = -1; x < columns + 1; x++)
{
for (int y = -1; y < rows + 1; y++)
{
GameObject toInstantiate = floorTiles[Random.Range(0, floorTiles.Length)];
if (x == -1 || x == columns || y == -1 || y == rows)
{
toInstantiate = outerWallTiles[Random.Range(0, outerWallTiles.Length)];
}
GameObject instance = Instantiate(toInstantiate, new Vector3(x, y, 0f), Quaternion.identity) as GameObject;
instance.transform.SetParent(boardHolder);
}
}
}
Vector3 RandomPosition()
{
int randomIndex = Random.Range(0, gridPositions.Count);
Vector3 randomPosition = gridPositions[randomIndex];
gridPositions.RemoveAt(randomIndex);
return randomPosition;
}
void LayoutObjectAtRandom(GameObject[] tileArray, int minimum, int maximum)
{
int objectCount = Random.Range(minimum, maximum + 1);
for (int i = 0; i < objectCount; i++)
{
Vector3 randomPosition = RandomPosition();
GameObject tileChoice = tileArray[Random.Range(0, tileArray.Length)];
Instantiate(tileChoice, randomPosition, Quaternion.identity);
}
}
public void SetupScene(int level)
{
BoardSetup();
InitialiseList();
LayoutObjectAtRandom(wallTiles, wallCount.minimum, wallCount.maximum);
LayoutObjectAtRandom(wallTiles, wallCount.minimum, wallCount.maximum);
int enemyCount = (int)Mathf.Log(level, 2f);
LayoutObjectAtRandom(enemyTiles, enemyCount, enemyCount);
Instantiate(exit, new Vector3(columns - 1, rows - 1, 0F), Quaternion.identity);
}
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameManager : MonoBehaviour
{
public BoardManager boardScript;
private int level = 3;
void Awake()
{
boardScript = GetComponent<BoardManager>();
InitGame();
}
void InitGame()
{
boardScript.SetupScene(level);
}
// Update is called once per frame
void Update()
{
}
}
Answer by jzylinski · Aug 25, 2019 at 02:08 PM
Not certain if this is the right solution @teabag5, but I'm having the same problem and I saw someone else mentioned on a threat removing the namespace Completed { line, as well as the closing } associated with it. That removed those errors for me, but I then got a NEW error:
NullReferenceException: Object reference not set to an instance of an object GameManager.InitGame () (at Assets/Scripts/GameManager.cs:16) GameManager.Awake () (at Assets/Scripts/GameManager.cs:12)
I've tried both the versions of the GameManager script described in the video, as well doing a copy/paste of the example script provided (adding the } at the end that was omitted :P) and I still get the same error message.
Do you also get this error when you make the change? If you do not, please share your scripts with me so I can figure out what else I'm doing wrong. Hope this helps...