Search Unity

How to check if a property is missing or not set (none)

Discussion in 'Scripting' started by Kiupe, Mar 11, 2019.

  1. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    Hello,

    I'm trying to figure out how to know if properties, in my case audio clip properties, are missing, meaning the link with the clip has been lost, or if the value is none, meaning the clip was never set.

    Capture d’écran 2019-03-11 à 21.35.21.png

    I tried to use various scripts detecting missing references but none of them can make the difference between Missing and None.

    Obviously the editor can tell the difference but I did not find how I can do the same using an editor script.

    Any idea ?

    Thanks
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    "null" is the C# keyword that corresponds to empty inspector values, so:


    if (EndFeedback == null)
    {
    Debug.Log("EndFeedback is empty!");
    }


    Edit: Oh, I think I misunderstood the question. Unfortunately, I don't know the answer to what you were actually asking. :oops:
     
    Fenikkel likes this.
  3. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    I did get valuable help from Unity Slack. you can use reflection to retrieve the string value displayed by the editor :

    Code (CSharp):
    1. var prop = typeof(SerializedProperty).GetProperty("objectReferenceStringValue", BindingFlags.NonPublic | BindingFlags.Instance);
    2.                         var result = (string)prop.GetValue(sp, null);
    That way you can check if it's a none or missing value.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    IIRC, Unity overrides the == operator so that invalid Unity references compare as being equal to "null" even when they are not literally "null". (For instance, a reference to a GameObject that has been destroyed.)

    I believe you can use ReferenceEquals to bypass this and check whether the reference is actually null.
     
    richard_harrington and jartison like this.
  5. deram_scholzara

    deram_scholzara

    Joined:
    Aug 26, 2005
    Posts:
    1,043
    For future reference, this should do the trick:
    Code (CSharp):
    1. var isMissing = ReferenceEquals(myVariable, null) ? false : (myVariable ? false : true);
    just replace "myVariable" with the variable you want to check.

    It basically says:
    • Is the reference actually null?
      • Yes: It's not missing, it's just null.
      • No: Something is referenced, is the referenced object "Unity-null"?
        • No: It is not missing.
        • Yes: It is missing.
     
    Last edited: Apr 10, 2021
  6. gaps

    gaps

    Joined:
    Jan 1, 2014
    Posts:
    15
    I've settled with simply this for cases where I want to know if it's destroyed/missing and for cases I want to know it's really null (i.e. not just destroyed).
    Code (CSharp):
    1. bool isDestroyed = ((object)var) != null && !var;
    2. bool isReallyNull = ((object)var) == null;
    Note that (object) needs to be in lower case, to be the alias for System.Object, otherwise you will instead end up casting var to Unity.Object and this code will not work.
     
    paynob, csm12s and deram_scholzara like this.