- Home /
Find if no objects with tag exist
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."
Answer by DaveA · Apr 26, 2011 at 09:51 PM
Odd. I wonder if it returns an array of zero length? Could you try that? (Docs say null though)
Changing the central part to:
if (other.transform.position == nextWaypoint) { Destroy(other.gameObject);
GameObject[] waypointArray = GameObject.FindGameObjectsWithTag(waypointNumber);
if (waypointArray.Length == 0)
{
Debug.Log("null!");
allow$$anonymous$$ove = false;
}
nextWaypoint = waypointArray[1].transform.position; }
Still doesn't fix the problem :/ It ignored the "if (waypointArray...)" and jumps straight to "nextWaypoint..". The debug console then says "IndexOutOfRangeException: Array index is out of range."
Ah, badly formatted. I'll edit the post to give the code details to make it more clear to read
Sounds like it had 1 element. That would account for length != 0 and [1] being out of range (try [0])
This was the answer that lead me to the solution. The array was of 0 length but even so it took a while to figure out the problem. array[0] gave the current position because the waypoint wasn't actually destroyed at the time. So, destroying a waypoint flips a bool which then searches for new waypoints on the next update (by which point the old waypoint is fully destroyed).
If you need code clarification please send me a message.
Thank you for all the answers :)
Answer by Unamine · Apr 27, 2011 at 10:18 AM
Try this: if (!GameObject.FindGameObjectsWithTag(waypointNumber))
The sign of negation (!) should be placed early in the condition as the example
I can't do that as "The !' operator cannot be applied to operand of type
UnityEngine.GameObject[]'"
I guess this is because you can't check it on an array... Is there no way to check if an array is empty?
Answer by Unamine · Apr 28, 2011 at 09:54 AM
So try to store GameObject.FindGameObjectsWithTag (waypointNumber) and put a variable like this:
var waypointsNumber // is an array if (! waypointsNumber) { // Place your code here }
Your answer
![](https://koobas.hobune.stream/wayback/20220613125525im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate specific object from "FindGameObjectsWithTag" Array 0 Answers
FindGameobjectsWithTag -> Array 2 Answers
Array error - Index is less than 0... 3 Answers
Null Reference Exception After refering to an Array 1 Answer
checking for nulls in arrays 2 Answers