OnTriggerEnter is throwing two NullReferenceExceptions when anything collides with it

I’m fairly new to Unity, so sorry if this is simple, but I can’t find an answer despite about an hour of googling.

What confuses me is this script was working without errors earlier, I made a seemingly unrelated change, and now it’s broke. (the unrelated change was creating a new Tag and assigning that tag to a child of the object with this script on it)

Here’s the OnTriggerEnter function and it’s related List declaration. Line 4 is where the error happens.
(errors are also thrown on the related OnTriggerExit function)

    private List<Collider> rearConnectorList;

    private void OnTriggerEnter(Collider enteredCollider) {
        if (!rearConnectorList.Contains(enteredCollider)) {
            rearConnectorList.Add(enteredCollider);
            Debug.Log("Adding " + enteredCollider.tag);
        }
    }
private List<Collider> rearConnectorList=new List<Collider>();

ok cool, that fixes the issue. Thanks!

But now i’m wondering why it worked before? what changed? why did it change?
(yes, actually genuinely curious)

Because the list was there before. You could not have done anything without creating it in the first place.

I’ve contacted a time traveler and he says that what you think you did and what you actually did aren’t the same.

This seems almost unbelievable to me but I will accept this claim at face value.

The answer is always the same… ALWAYS!

Do not post for this error. Do not google for this error. Simply do these three steps:

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

I definitely believe that. lol

Ok, but how am i supposed to know about this easy 3-step process without googling for it in the first place?
Like I said, I’m new at this. I’m sorry I offended you with my problem.

Interesting. I am very sure I never had a ‘=new List()’ in my script before I received Zulo’s answer. That was an entirely new concept to me when I read it. (creating a new container on the same line I declared the container. I had thought the declaration implied it needed to create a new container)

Is it possible that Unity or VS was ‘smart’ enough to just create the new list for me the first time? I have had other systems do that for me in the past, such as VBA macros.

Either way, thanks for the knowledge! I appreciate it.

No, you likely had it serialised, of which will prevent it being null, as Unity’s default serialisation doesn’t support null.

public class ExampleMonobehaviour : Monobehaviour
{
    private List<Collider> exampleListOne; // will be null
   
    [SerializeField]
    private List<Collider> exampleListTwo; // will not be null
   
    public List<Collider> ExampleListThree; // will not be null
}

Thats it! Thank you very much!

See? The time traveler was right! lol