我有两个脚本,我希望它们互相访问,分别将bool设置为true和false。但我收到此错误:NullReferenceException:对象引用未设置为对象的实例
这是第一个脚本:
using UnityEngine;
using System.Collections;
public class Colliders_overkant : MonoBehaviour
{
public bool overkantActive;
GameObject personage;
GameObject eerstekant;
void Start () {
overkantActive = true;
}
void Update () {
}
void OnTriggerEnter ( Collider collider )
{
if (collider.tag == "Speler")
{
if (overkantActive == true)
{
Debug.Log ("Overkant");
eerstekant = GameObject.FindWithTag ("Eerstekant");
personage = GameObject.FindWithTag ("Speler");
personage.transform.Rotate (0 , 180, 0);
gameObject.GetComponent<Colliders_eerstekant>().eerstekantActive = true;
overkantActive = false;
}
}
}
}
而另一个是:
using UnityEngine;
using System.Collections;
public class Colliders_eerstekant : MonoBehaviour
{
public bool eerstekantActive;
GameObject personage;
GameObject overkant;
void Start () {
eerstekantActive = false;
}
void Update () {
}
void OnTriggerEnter ( Collider collider )
{
if (collider.tag == "Speler")
{
if (eerstekantActive == true)
{
Debug.Log ("Eerstekant");
personage = GameObject.FindWithTag ("Speler");
overkant = GameObject.FindWithTag ("Overkant");
personage.transform.Rotate (0 , 180, 0);
gameObject.GetComponent<Colliders_overkant>().overkantActive = true;
eerstekantActive = false;
}
}
}
}
答案 0 :(得分:0)
正如它在锡上所说,你试图访问设置为null的变量的属性和方法。您使用的以下语句如果无法找到指示要查找的内容,则返回null:
GameObject.FindWithTag ("Speler");
gameObject.GetComponent<Colliders_overkant>();
第一个意味着在统一场景中没有带有标签的游戏对象&#34; Speler&#34;如果它返回null,则第二个意味着gameObject上没有名为&#34; Colliders_overkant&#34;如果它返回null。