Hi there,
I have a list of GameObjects over which I want to do some operations. I’ve saved the list into a private variable as such:
Creatures = GameObject.FindGameObjectsWithTag("Creature") ;
Elsewhere in my code, I’m trying to store information about the creatures into a list.
private List<float> GetVelocities(GameObject[] creatureList) {
List<float> temp = new List<float>();
foreach(GameObject creature in creatureList) {
temp.Add(creature.GetComponent<CreatureAttributes>().Velocity);
}
return temp;
}
The problem is that if a creature is destroyed in between, a NullReferenceException is made.
This can be solved by putting an if statement to check if the creature is not null before trying to access it’s field, but this seems somewhat inelegant.
Is there a better solution for checking if the object is not null?
Thanks.