0

My first question here. I'll try to be clear :) Is there a way to get the value of a variable in a script, on a GameObject in an array? I have an array of GameObjects, on which each of them has the same script attached (ExampleScript). In the ExampleScript there is a int variable (exampleInt), that is generated within the script. I want to print out each of the exampleInt values, for each of the GameObjects. My code (which results in a NullReferenceException) is:

public GameObject[] exampleGo; //all the GameObjects are manually added in Inspector

private void Start()
{
    foreach (GameObject go in exampleGo)
    {
         Debug.Log(go.GetComponent<ExampleScript>().exampleInt);
    }
}

Can you help me Stack Overflow, you are my only hope?

I've tried delaying the script, in case something hasn't been generated when I run the code above.

I'm just barely understanding NullReferenceExceptions, but this one is trickier than my earlier encounters with it for some reason.

5
  • Could you edit your question with the exact error message, copy-pasted? An NRE here could mean one of three things, either your exampleGo array is null (unlikely, if you set it in the inspector), your array contains a null gameobject (labeled as "None" or "Missing" in the inspector), or one of the objects you put in the array don't have an ExampleScript. Commented Dec 19, 2024 at 19:54
  • Regardless of what's null here, you could avoid the GetComponent<ExampleScript>() by changing public GameObject[] exampleGo; to public ExampleScript[] exampleGo;. Then, you can loop over the instances of these components directly, instead of looping over the gameobjects attached to them. Commented Dec 19, 2024 at 19:55
  • GetComponent<ExampleScript>() is likely results in null reference, althugh weird,I would have think that implementation of such thing would use a null-object pattern. Commented Dec 19, 2024 at 19:56
  • Thank you for you insights! I'll experiment with those answers. Thank you! How can I best thank you? I don't see a "like"-button or something? Commented Dec 19, 2024 at 19:59
  • @KarlSköld We were only guessing at your solution because there wasn't really enough context to be 100% confident, but I've posted it as a proper answer that you can accept. Commented Dec 20, 2024 at 0:04

1 Answer 1

0

An NRE here could mean one of three things, either your exampleGo array is null (unlikely, if you set it in the inspector), your array contains a null gameobject (labeled as "None" or "Missing" in the inspector), or one of the objects you put in the array doesn't have an ExampleScript attached.

Regardless of what's null here, you could avoid the GetComponent<ExampleScript>() by changing your array to be of type ExampleScript[]

public ExampleScript[] exampleGo;

Then, you can loop over the instances of these components directly, instead of looping over the gameobjects attached to them.

Sign up to request clarification or add additional context in comments.

1 Comment

It is most probably the latter .. if there was an object missing in the array Unity would rather through a custom MissingReferenceException .. using the correct type right away not only is more efficient but also prevents from referencing any object without that component in the first place

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.