1.在one1脚本中获取one2代码的数值时,为什么在Start()中写game1 = GetComponent();会报错(NullReferenceException: Object reference not set to an instance of an object
One1.Update () (at Assets/Scripts/One1.cs:22)
),而在update()中直接用game1.GetComponent().Mesh(a1);调用方法没有问题。(只要一写game1 = GetComponent();无论后面updata()中是否写game1.GetComponent().Mesh(a1);都会报同样的错。)
2.为什么在One2脚本中打印的是:2:0,1:1。而不是2:1,1:1。按道理说a2的值应该传入出去的但为什么没有传入?该怎么改让a2的值为1。即打印的是2:1。
其中已经确认是a1正确传入过来,外部接口拖入正确,带相关脚本的游戏物体正确配置,且启用。
相关代码:
One1中:
public class One1 : MonoBehaviour
{
public One2 game1;//带有脚本One2的游戏物体
private float a1=1;
private void Awake()
{
game1 = GetComponent<One2>();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
game1.GetComponent<One2>().Mesh(a1);
}
}
One2中:
public class One2 : MonoBehaviour
{
private float a2=0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Debug.Log("2:"+a2);
}
public void Mesh(float a)
{
a2 = a;
Debug.Log("1:"+a);
}
}