Zero coalescing operator assignment to itself

I currently have two scripts configured in Unity to handle some UI Audio. One of them is a manager, and the other is to play a specific user interface element. A simplified version of what I have is:

public class AudioUIManager : MonoBehaviour //Only one of these in the scene
{
    public AudioClip genericUISound; //This is set in the inspector.
}

public class AudioUITextAnimation : MonoBehaviour
{
    [SerializeField]
    private AudioClip specifiedUISound; //This is not set in the inspector
    [SerializeField]
    private AudioUIManager audioUIManager; // I get a reference to this elsewhere

    void Start()
    {
        //Use generic sounds from audio manager if nothing is specified.
        specifiedUISound = specifiedUISound ?? audioUIManager.genericUISound;
        print(specifiedUISound);
    }
}

What I'm trying to achieve here is for the field to specifiedUISounduse the sound assigned to it by the inspector. If no sound is assigned, use general sound from the user interface manager. This allows me to assign the same sound to millions of buttons that require the same sound, but it gives me the ability to have a specific sound for one button if I want.

null-coalessing null to specifiedUISound, null, audioUIManager - . , , :

specifiedUISound = specifiedUIsound == null ? audioUIManager.genericUISound : specifiedUISound;

? ?

: , specifiedUISound. ( public [SerializeField]). - , ?

+4
2

Unity :

MonoBehaviour , "real null", "fake null". == , - , . , , . NullReferenceException, , , GameObject MonoBehaviour, , .

Unity null , null. .

specifiedUISound null? , . # "", .

, : == Object.Equals

, : ?? Object.ReferenceEquals

. null null.

+7

, , , SerializeField.

(someObj nullObj ):

    public AudioClip someObj;

    [SerializeField]
    private AudioClip nullObj;

    void Start () 
    {
        Debug.Log("nullObj == null : " + (nullObj == null));
        Debug.Log("someObj == null : " + (someObj == null));

        nullObj = nullObj ?? someObj;

        Debug.Log ("nullObj == null : " + (nullObj == null));
    }

:

enter image description here

, SerializeField, -:

    public AudioClip someObj;

    private AudioClip nullObj;

    void Start () 
    {
        Debug.Log("nullObj == null : " + (nullObj == null));
        Debug.Log("someObj == null : " + (someObj == null));

        nullObj = nullObj ?? someObj;

        Debug.Log ("nullObj == null : " + (nullObj == null));
    }

:

enter image description here

, :

, , Unity3D Mono. , , , , - == seriazlized

, , .

+2

All Articles