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)
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
}