I have a script that is attached to all the NPCs in my game.
Here's what the variable declarations and Awake() function look like:
Dialogue dialogue;
TextMeshProUGUI UIText;
GameObject textBox;
Image portrait;
AudioSource audioSource;
private void Awake()
{
audioSource = GameObject.Find("Audio Source").GetComponent<AudioSource>();
UIText = GameObject.Find("Canvas").GetComponentInChildren<TextMeshProUGUI>();
textBox = GameObject.Find("TextBox");
portrait = GameObject.Find("Portrait").GetComponent<Image>();
textBox.SetActive(false);
}
All of this works fine, until it gets to
portrait = GameObject.Find("Portrait").GetComponent<Image>();
For some reason, it throws a NullReferenceException on that line.
Two things about this confuse me:
The GetComponents above (for the audioSource and UIText) appear to work fine, despite being called for each of the NPCs. I'd assume it'd either work for all or none of them.
The portrait variable is used to show a portrait of the NPC when the player is talking to them, and even with the errors, one of the NPCs still has their portrait. It's like one NPC grabs the Image component, and none of the others can snag it or something...?
Anyway, very grateful for any wisdom y'all may provide, and happy to provide any other info that could lead to a resolution. Thanks! Hope your new year is going well!
EDIT: There is a Portrait GameObject in my scene under the Canvas, with an Image component attached to it. This I can confirm.