I have several waypoints with a tag "waypoint1" with govern the movement. However, I would like to check if any of them exist before attempting to move.
The problem I have is that it completely ignored the 'if null' check. How can I get that to work?
waypointNumber is a string.
This is my current code which doesn't work
void OnTriggerStay(Collider other)
{
if (other.tag == waypointNumber)
{
Debug.Log("hit");
if (other.transform.position == nextWaypoint)
{
Destroy(other.gameObject);
if (GameObject.FindGameObjectsWithTag(waypointNumber) == null)
{
Debug.Log("null!");
allowMove = false;
}
GameObject[] waypointArray = GameObject.FindGameObjectsWithTag(waypointNumber);
nextWaypoint = waypointArray[1].transform.position;
}
}
}
I have tried using:
if (GameObject.FindGameObjectsWithTag(waypointNumber) != null{}else{DoStuff}
With no results.
I tried the waypointArray.Length == 0 like this:
if (other.transform.position == nextWaypoint)
{
Destroy(other.gameObject);
GameObject[] waypointArray = GameObject.FindGameObjectsWithTag(waypointNumber);
if (waypointArray.Length == 0)
{
Debug.Log("null!");
allowMove = false;
}
nextWaypoint = waypointArray[1].transform.position;
}
It ignored the "if (waypointArray.Length == 0)" and jumped straight to nextWaypoint = ... The console gave the error of "IndexOutOfRangeException: Array index is out of range."