Why are there no enemies spawning in the 2d roguelike tutorial I am doing?
I am on the 5th lesson of the 2d Roguelike tutorial and I am having the issue of all of my other game objects instantiate correctly, yet the enemies do not. It looks like the code is correct. I have retyped it several times. If I drag a new prefab directly into the Scene the sprite shows up just fine. Any ideas as to why the sprites are not showing up when Board Manager tries to instantiate them? I included the code for reference.
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(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 InitaliseList()
{
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();
InitaliseList();
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);
}
}
I have been trying to fix this and have not had any luck. The LayoutObjectAtRandom method is working. If I put other sprites into the enemyTiles array they appear just fine. If I drag the enemy Prefabs into the scene without going through the Array and $$anonymous$$ethod, they animate and stay just fine.
Answer by DukeFutzwillus · Dec 18, 2016 at 09:02 PM
Never mind, I figured out that the sorting layer was set to Default instead of Units and that was causing the issue.
Your answer
Follow this Question
Related Questions
checking if gameobject exists 1 Answer
How to Serialize or Save a List of GameObjects 1 Answer
Unnamed gameobjects appear in Unity4 when opening Unity5 project 0 Answers
Set Click area in Game 1 Answer
Accessing values on a gameobject 1 Answer