After completing the class 142 and 143 there has been errors in my console for script PathFinder , Enemymover and ObjectPool.
The Error Lines For “Enemy Mover” is at the bottom of PathFinder script copy
Pathfinder c# - error lines 88 in BreathFIrst Meathod = destinationNode.isWalkable = true;
** line 56 in GetNewPath = BreathFirstSearch();**
Script-
public class PathFinder : MonoBehaviour
{
[SerializeField] Vector2Int startCoordinate;
public Vector2Int StartCoordinate
{
get { return startCoordinate; }
}
[SerializeField] Vector2Int destinationCoordinate;
public Vector2Int DestinationCoordinate
{
get { return destinationCoordinate; }
}
Node startNode;
Node destinationNode;
Node currentSearchNode;
Queue<Node> frontier = new Queue<Node>(); //QUEUE
Dictionary<Vector2Int, Node> reached = new Dictionary<Vector2Int, Node>(); //REACHED
Vector2Int[] directions = { Vector2Int.right, Vector2Int.left, Vector2Int.up, Vector2Int.down };
GridManager gridManager;
Dictionary<Vector2Int, Node> grid = new Dictionary<Vector2Int, Node>();
void Awake()
{
gridManager = FindObjectOfType<GridManager>();
if (gridManager != null)
{
grid = gridManager.Grid;
startNode = grid[startCoordinate];
destinationNode = grid[destinationCoordinate];
}
}
void Start()
{
GetNewPath();
}
public List<Node> GetNewPath()
{
gridManager.ResetNode();
BreathFirstSearch();
return BuildPath();
}
void ExploreNeighbors()
{
List<Node> neighbors = new List<Node>();
foreach (Vector2Int direction in directions)
{
Vector2Int neighborCoordinate = currentSearchNode.coordinates + direction;
if (grid.ContainsKey(neighborCoordinate))
{
neighbors.Add(grid[neighborCoordinate]);
}
}
foreach (Node neighbor in neighbors)
{
if (!reached.ContainsKey(neighbor.coordinates) && neighbor.isWalkable)
{
neighbor.connectedTo = currentSearchNode;
reached.Add(neighbor.coordinates, neighbor);
frontier.Enqueue(neighbor);
}
}
}
void BreathFirstSearch()
{
startNode.isWalkable = true;
destinationNode.isWalkable = true;
frontier.Clear();
reached.Clear();
bool isRunning = true;
frontier.Enqueue(startNode);
reached.Add(startCoordinate, startNode);
while (frontier.Count > 0 && isRunning)
{
currentSearchNode = frontier.Dequeue();
currentSearchNode.isExplored = true;
ExploreNeighbors();
if (currentSearchNode.coordinates == destinationCoordinate)
{
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 = true;
grid[coordinates].isWalkable = false;
List<Node> newPath = new List<Node>();
grid[coordinates].isWalkable = true;
if (newPath.Count <= 1)
{
GetNewPath();
return true;
}
}
return false;
}
}
**ENEMYMOVER C# - error line 37 in FindPath -path = pathFinder.GetNewPath();**
** line 27 in OnEnable - FindPath();**
Script=
public class EnemyMover : MonoBehaviour
{
[SerializeField] [Range(0f,5f)] float speed = 1f;
List<Node> path = new List<Node>();
Enemy enemy;
GridManager gridManager;
PathFinder pathFinder;
void Awake()
{
enemy = GetComponent<Enemy>();
gridManager = FindObjectOfType<GridManager>();
pathFinder = FindObjectOfType<PathFinder>();
}
void OnEnable()
{
FindPath();
ReturnToStart();
StartCoroutine(FollowPath());
}
void FindPath()
{
path.Clear();
path = pathFinder.GetNewPath();
}
void ReturnToStart()
{
transform.position = gridManager.GetPositionFromCoordinate(pathFinder.StartCoordinate);
}
void FinishPath()
{
enemy.StealGold();
gameObject.SetActive(false);
}
IEnumerator FollowPath()
{
for (int i = 0; i < path.Count; i++)
{
Vector3 startPosition = transform.position;
Vector3 endPosition = gridManager.GetPositionFromCoordinate(path[i].coordinates);
float travelPercentage = 0f;
transform.LookAt(endPosition);
while (travelPercentage < 1f)
{
travelPercentage += Time.deltaTime * speed;
transform.position = Vector3.Lerp(startPosition, endPosition, travelPercentage);
yield return new WaitForEndOfFrame();
}
}
FinishPath();
}
}
OBJECTPOOL C# - line 40 in EnableObjectInPool - pool[i].SetActive(true);
** line 50 in IEnumerator SpawnEnemy - EnableObjectInPool();**
Script -
public class ObjectPool : MonoBehaviour
{
[SerializeField] GameObject enemyPrefab;
[SerializeField][Range(0f ,50f)] int poolSize = 5;
[SerializeField][Range(0.1f , 30f)] float spawnTimer =1f;
GameObject[] pool;
void Awake()
{
PopulatePool();
}
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);
}
}
}