- Home /
Empty property through reflection is not null
I'm trying to list a component's properties that are not null. However currently my code does not filter out the null components.
(By the way the "how to ask a question" video won't load so I'm not sure how to get the code to format pleasantly).
Example:
object value = collider.GetType().GetProperty("particleSystem").GetValue(collider, null);
Debug.Log(value.GetType());
if (value != null || !string.IsNullOrEmpty(value.ToString()))
{
Debug.Log("particleSystem = " + value);
}
//Output:
//UnityEngine.ParticleSystem
//UnityEngine.Debug:Log(Object)
//particleSystem = null
//UnityEngine.Debug:Log(Object)
Um, should get a null reference exception on the Debug.Log if it was null right?
The word "null" when converted to a string is not null or empty I'd guess!
Yes of course, thanks. It works with value.ToString() != "null".
I'm wondering why an unset property is not null and ins$$anonymous$$d returns the string "null".
and even more bizarre, why does value.GetType() return the proper type?!?! ( it does, I just ran into this myself ). It's like unity is filling in some dummy value for the property that masquerades as null but is somehow typed.
Yeah it does that :D It returns null for things which have been destroyed even if the reference is still set...
just found this, kinda strange, am I correct in my reading that the correct behavior is to check for a string that is == to "null" for an unset property on a GO like rigid body?
Your answer
![](https://koobas.hobune.stream/wayback/20220613101227im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to define component properties globally 1 Answer
FieldInfo.GetValue with GameObject field 0 Answers
2D Animation does not start 1 Answer