I’m looking for help with something very simple. I have been following this tutorial:
I got to “Checked with version: 5 - Difficulty: Intermediate”
At 3:23 he asked us to test the level. As you can see in the video objects show up
Now my issue is the objects are simply not being instanciated despite my code having no noticeable errors
The issue is that none of my functions (except for the update function) are being called. This MAKES SENSE because at no point did I call or reference it so it’s no surprise that it didn’t work. Yet in the video, the script worked for our teacher just fine. So how do I make my level work like the video?
Here is my GameManager code
using UnityEngine;
using System.Collections;
//using System.Collections.Generic;
public class GameManager : MonoBehaviour
{
public BoardManager boardScript;
private int level = 3;
// Start is called before the first frame update
void awake()
{
Debug.Log("AWAKE");
boardScript = GetComponent<BoardManager>();
InitGame();
}
void InitGame()
{
Debug.Log("INITGAME");
boardScript.SetupScene(level);
}
// Update is called once per frame
void Update()
{
//Debug.Log("UPDATE");
}
}
Here is my Board Manager Code
using System.Collections;
using UnityEngine;
using System;
using System.Collections.Generic;
using Random = UnityEngine.Random;
public class BoardManager : MonoBehaviour
{
[Serializable]
public class Count
{
public int minimum;
public int maximum;
public Count (int min, int max)
{
minimum = min;
maximum = max;
}
}
public int columns = 8;
public int rows = 8;
public Count wallCount = new Count (5,9);
public Count foodCount = new Count (5,9);
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 InitialiseList()
{
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 ()
{
Debug.Log("SETUP");
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(foodTiles, foodCount.minimum, foodCount.maximum);
int enemyCount = (int)Mathf.Log(level,2f);
LayoutObjectAtRandom(enemyTiles, enemyCount, enemyCount);
Instantiate(exit, new Vector3(columns - 1, rows - 1, 0f), Quaternion.identity);
}
}