Unity 踩坑笔记 ??的使用问题

如果我写成这样

public class TestNull : MonoBehaviour
{
    
    
	public AnimationClip clip1;
	public AnimationClip clip2;
	void Start()
	{
    
    
		var clip = clip1 ?? clip2; // 此处
		if(clip == null)
		{
    
    
			Debug.Log("---- null ----");
		}
		else
		{
    
    
			Debug.Log(clip.name);
		}
	}
}

然后这样引用,会取不到clip2

在这里插入图片描述

解决:
改用三元运算符

var clip = clip1 != null ? clip1 : clip2

猜你喜欢

转载自blog.csdn.net/EdmundRF/article/details/130132620