redbluegames/unity-notnullattribute

NotNullFinder fails to report Null entries on Arrays

edwardrowe opened this issue · 6 comments

When you apply a NotNullAttribute to an array, each element gets the warning in the editor as expected, which is sweet. However, when doing the "NotNull" search no errors are given for null members.

Expected behaviour (WIP):

  1. Arrays should error for every member that is not wired up.
  2. Arrays that have no size should not assert. This is admittedly strange because why is an array of 0 given a require wire attribute? In general, requireWire doesn't make much sense on arrays.
  3. Another implementation is to have an ArrayNotNull attribute that requires the array is a certain size.

Would really love that, is that something you can add?

Thanks for both of your feedback - it's helpful to know what people would want with it, and that people are using this!

I don't have a lot of bandwidth right now to work on this project, but this should be doable since NotNull uses a PropertyDrawer (which replaces the way the property is drawn entirely, so we can do whatever we want).

Here's some notes to get future me started (or anyone who wants to do a Pull Request for Hacktoberfest!)
We'd want to replace the way the property is drawn for arrays (it's drawn here:

private void BuildObjectField(Rect drawArea, SerializedProperty property, GUIContent label)
). Thinking we'd need to use serializedProperty.Next(enterChild) to draw each element with an optional '*' and info box. We'd also need to report the height of the property based on how many elements have errors (and if it's expanded)

Hey @edwardrowe,

Actually, for me there is no problem with the inspector itself, it shows pretty well the null warning even within arrays and dictionaries.

However, the 'Not Null Finder' is a different story, it seems like that ReflectionUtility.GetFieldsWithAttributeFromType won't return fields within arrays/dictionaries.

Ok I am able to see what you're saying. I'm going to rename this issue to better reflect the problem.

Also some repro steps to clarify:

  1. Create a MonoBehaviour with a serialized array
  2. Mark the array [NotNull] (and public / serialized)
  3. Create a GameObject instance that uses the MB from step 1
  4. Add a few elements to the array. Note each element shows a warning, as expected.
  5. Run NotNullFinder. Observe the fields don't report in the Console.

I'd expect a log entry like:

[NotNullViolation: Field=Array/Element0, FullName=MyGameObjectInstance]

Apologies for the confusion. My fault totally - the description of the task says it right front and center what you're talking about.