晚上好,在学习unity脚本传值时遇到了这个问题

两个Demo(Demo4与Demo5)。

其中,要将Demo5的IntDiamond的值(20)传给Demo4的IntDisplayDiamondNUmber。
分别建立两个空对象——Empty4和Empty5,
并且做如下操作:
将Demo4——Empty4;
Demo5——Empty5;
将Empty5给到Empty4的Demo4脚本中,如下图

img


然后结果为钻石的数量是:0,而不是20。
不知道怎么做。

/*项目简介
*学习脚本传值,传给Demo4;
*
*
*
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
    public int IntDiamond = 20;
    public int Display()
    {
        return 100;
    }

    void Start()
    {
        
    }

    
    void Update()
    {
        
    }
}


/*项目简介
*学习脚本传值,接受Demo5;
*
*
*
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo4 : MonoBehaviour
{
    public GameObject goDisplay;
    private int IntDisplayDiamondNUmber;
    void Start()
    {
        if (goDisplay)
        {
            IntDisplayDiamondNUmber = goDisplay.GetComponent<Demo5>().IntDiamond;

        }
        Debug.Log("[Demo4.cs]钻石的数量是:" + IntDisplayDiamondNUmber);
    }

    
    void Update()
    {
        
    }
}


希望各位有时间麻烦帮帮忙,谢谢啦。

看一下你的Demo5的钻石数量,在Inspector面板是不是0,而不是你代码中的20。

img