The following function gives me null whenever I call it.
List<Avatar> party = GameObject.FindObjectOfType<Party> ().GetAvatarsInParty ();
foreach (Avatar avatar in party)
if (avatar.name.Contains ("AvatarName"))
It’s definitely hitting return avatar, and print(avatar) is successfully printing the name of the FieldAvatar I want.
The only explanation is that I’m an idiot and that I don’t understand something basic about how this is meant to work.
There are generally two cases i can imagine when seeing that portion of code:
- Either that “Avatar” object that you return is derived from MonoBehaviour (or ScriptableObject) and got destroyed or wrongly initialized.
- You might call that method several times and the one that matters returned null because the list might have been empty at that point.
Both cases can only be verified by you as the necessary information is missing in your question.
If this answer doesn’t solve your problem, make sure you add more details to your question. Things of interest would be:
- What is that Avatar you are returning? Most likely a class. Is it derived from something?
- Where / when is the list (that is returned by “GetAvatarsInParty” ) inside your “Party” class initialized?
- Try adding another Debug.Log / print after your for loop to see when no avatar was found.