- Home /
Question by
doggycatboy901 · Jul 03, 2021 at 07:26 AM ·
parameters
Optional List Parameter not working
Hi I'm having trouble using optional parameters and in this case a list. Example:
Method([Optional] List optionalList) { GameObject test = new GameObject; optionalList.Add(test ); }
There are no input errors, but when I run it with a method which doesn't input the optionalist, it throws an error when test is added.
Comment
Best Answer
Answer by doggycatboy901 · Jul 02, 2021 at 07:15 PM
Solution: It passes null if you don't input anything, so just do a null check: if (optionalList != null) optionalList.add(test);