Hello, so after changing the execution order in Unity a few errors appeared, please help.
What is going on?
Also no enemies are spawning at all at this point.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridManager : MonoBehaviour
{
[SerializeField] Vector2Int gridSize;
[Tooltip("World Grid Size - Should match UnityEditor snap settings.")]
[SerializeField] int unityGridSize = 10;
public int UnityGridSize { get { return unityGridSize; } }
Dictionary<Vector2Int, Node> grid = new Dictionary<Vector2Int, Node>();
public Dictionary<Vector2Int, Node> Grid { get { return grid; } }
void Awake()
{
CreateGrid();
}
public Node GetNode(Vector2Int coordiates)
{
if (grid.ContainsKey(coordiates))
{
return grid[coordiates];
}
return null;
}
public void BlockNode(Vector2Int coordinates)
{
if (grid.ContainsKey(coordinates))
{
grid[coordinates].isWalkable = false;
}
}
public void ResetNodes()
{
foreach(KeyValuePair<Vector2Int, Node> entry in grid)
{
entry.Value.connectedTo = null;
entry.Value.isExplored = false;
entry.Value.isPath = false;
}
}
public Vector2Int GetCoordinatesFromPosition(Vector3 position)
{
Vector2Int coordinates = new Vector2Int();
coordinates.x = Mathf.RoundToInt(position.x / unityGridSize);
coordinates.y = Mathf.RoundToInt(position.z / unityGridSize);
return coordinates;
}
public Vector3 GetPositionFromCoordinates(Vector2Int coordinates)
{
Vector3 position = new Vector3();
position.x = coordinates.x * unityGridSize;
position.z = coordinates.y * unityGridSize;
return position;
}
void CreateGrid()
{
for(int x = 0; x < gridSize.x; x++)
{
for (int y = 0; y < gridSize.y; y++)
{
Vector2Int coordinates = new Vector2Int(x, y);
grid.Add(coordinates, new Node(coordinates, true));
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pathfinder : MonoBehaviour
{
[SerializeField] Vector2Int startCoordinates;
public Vector2Int StartCoordinates { get { return startCoordinates; } }
[SerializeField] Vector2Int destinationCoordinates;
public Vector2Int DestinationCoordinates { get { return destinationCoordinates; } }
Node startNode;
Node destinationNode;
Node currentSearchNode;
Queue<Node> frontier = new Queue<Node>();
Dictionary<Vector2Int, Node> reached = new Dictionary<Vector2Int, Node>();
Vector2Int[] directions = { Vector2Int.right, Vector2Int.left, Vector2Int.up, Vector2Int.down };
GridManager gridManager;
Dictionary<Vector2Int, Node> grid = new Dictionary<Vector2Int, Node>();
private void Awake()
{
gridManager = FindObjectOfType<GridManager>();
if(gridManager != null)
{
grid = gridManager.Grid;
startNode = grid[startCoordinates];
destinationNode = grid[destinationCoordinates];
}
}
// Start is called before the first frame update
void Start()
{
GetNewPath();
}
public List<Node> GetNewPath()
{
gridManager.ResetNodes();
BreadthFirstSearch();
return BuildPath();
}
void ExploreNeighbors()
{
List<Node> neighbors = new List<Node>();
foreach(Vector2Int direction in directions)
{
Vector2Int neighborCoords = currentSearchNode.coordinates + direction;
if (grid.ContainsKey(neighborCoords))
{
neighbors.Add(grid[neighborCoords]);
}
}
foreach(Node neighbor in neighbors)
{
if(!reached.ContainsKey(neighbor.coordinates) && neighbor.isWalkable)
{
neighbor.connectedTo = currentSearchNode;
reached.Add(neighbor.coordinates, neighbor);
frontier.Enqueue(neighbor);
}
}
}
void BreadthFirstSearch()
{
startNode.isWalkable = true;
destinationNode.isWalkable = true;
frontier.Clear();
reached.Clear();
bool isRunning = true;
frontier.Enqueue(startNode);
reached.Add(startCoordinates, startNode);
while(frontier.Count > 0 && isRunning)
{
currentSearchNode = frontier.Dequeue();
currentSearchNode.isExplored = true;
ExploreNeighbors();
if(currentSearchNode.coordinates == destinationCoordinates)
{
isRunning = false;
}
}
}
List<Node> BuildPath()
{
List<Node> path = new List<Node>();
Node currentNode = destinationNode;
path.Add(currentNode);
currentNode.isPath = true;
while(currentNode.connectedTo != null)
{
currentNode = currentNode.connectedTo;
path.Add(currentNode);
currentNode.isPath = true;
}
path.Reverse();
return path;
}
public bool WillBlockPath(Vector2Int coordinates)
{
if (grid.ContainsKey(coordinates))
{
bool previousState = grid[coordinates].isWalkable;
grid[coordinates].isWalkable = false;
List<Node> newPath = GetNewPath();
grid[coordinates].isWalkable = true;
if(newPath.Count <= 1)
{
GetNewPath();
return true;
}
}
return false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Enemy))]
public class EnemyMover : MonoBehaviour
{
[SerializeField][Range(0f, 5f)] float speed = 1f;
List<Node> path = new List<Node>();
Enemy enemy;
GridManager gridManager;
Pathfinder pathfinder;
// Start is called before the first frame update
void OnEnable()
{
FindPath();
ReturnToStart();
StartCoroutine(FollowPath());
}
private void Awake()
{
enemy = GetComponent<Enemy>();
gridManager = FindObjectOfType<GridManager>();
pathfinder = FindObjectOfType<Pathfinder>();
}
void FindPath()
{
path.Clear();
path = pathfinder.GetNewPath();
}
void ReturnToStart()
{
transform.position = gridManager.GetPositionFromCoordinates(pathfinder.StartCoordinates);
}
void FinishPath()
{
enemy.StealGold();
gameObject.SetActive(false);
}
IEnumerator FollowPath()
{
for(int i = 0; i< path.Count; i++)
{
Vector3 startPosition = transform.position;
Vector3 endPosition = gridManager.GetPositionFromCoordinates(path[i].coordinates);
float travelPercent = 0f;
transform.LookAt(endPosition);
while (travelPercent < 1f)
{
travelPercent += Time.deltaTime * speed;
transform.position = Vector3.Lerp(startPosition, endPosition, travelPercent);
yield return new WaitForEndOfFrame();
}
}
FinishPath();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectPool : MonoBehaviour
{
[SerializeField] GameObject enemyPrefab;
[SerializeField] [Range(0, 50)] int poolSize = 5;
[SerializeField] [Range(0.1f, 30f)] float spawnTimer = 1f;
GameObject[] pool;
private void Awake()
{
PopulatePool();
}
// Start is called before the first frame update
void Start()
{
StartCoroutine(SpawnEnemy());
}
void PopulatePool()
{
pool = new GameObject[poolSize];
for(int i = 0; i < pool.Length; i++)
{
pool[i] = Instantiate(enemyPrefab, transform);
pool[i].SetActive(false);
}
}
void EnableObjectInPool()
{
for(int i = 0; i < pool.Length; i++)
{
if(pool[i].activeInHierarchy == false)
{
pool[i].SetActive(true);
return;
}
}
}
IEnumerator SpawnEnemy()
{
while (true)
{
EnableObjectInPool();
yield return new WaitForSeconds(spawnTimer);
}
}
}