Exception occurs when the destination set to the player castle gate

I am running into an exception error when running the script with the destination set to the player castle gate. If I set the destination to any other valid space, the script functions fine. Upon further investigation, I noticed that the pathfinder script deactivates when the destination coordinates is set to the player gate. I don’t see any reason for this to occur.

Below is the contents of the pathfinder script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tower : MonoBehaviour
{
    [SerializeField] int cost = 75;
    float buildDelay = 1f;

    void Start()
    {
        StartCoroutine(Build());
    }

    public bool CreateTower(Tower tower, Vector3 position)
    {
        Bank bank = FindObjectOfType<Bank>();

        if(bank == null)
        {
            return false;
        }

        if (bank.CurrentBalance >= cost)
        {
            Instantiate(tower.gameObject, position, Quaternion.identity);
            bank.Withdraw(cost);
            return true;
        }
        
        return false;

    }

    IEnumerator Build()
    {
        
        foreach(Transform child in transform)
        {
            child.gameObject.SetActive(false);

            foreach(Transform grandChild in child)
            {
                grandChild.gameObject.SetActive(false);
            }
        }

        foreach (Transform child in transform)
        {
            child.gameObject.SetActive(true);
            yield return new WaitForSeconds(buildDelay);
            foreach(Transform grandChild in child)
            {
                grandChild.gameObject.SetActive(true);
            }
        }


    }
}

I just figured out the issue, the grid size on the Grid Manager was 1 size smaller that it needed to be. The destination is at the very edge of the play area and the Grid Manager includes row/column 0 in its size. This works as normal now.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms