2015-08-16 66 views
-2

我的脚本...为什么我一直得到这个NullReferenceException错误?

using UnityEngine; 
using System.Collections; 

public class CountDownTimer : MonoBehaviour { 

    public int score; 
    float timeRemaining = 15; 

    // Use this for initialization 
    void Start() { 
    } 

    // Update is called once per frame 
    void Update() { 
     timeRemaining -= Time.deltaTime; 
     score = GetComponent<TriggerZone>().score; 
    } 

    void OnGUI(){ 
     if (timeRemaining > 0) { 
      GUI.Label(new Rect(100, 100, 200, 100), "Time Remaining: "+(int)timeRemaining); 
     } 
     else{ 
      GUI.Label(new Rect(100, 100, 200, 100), "Times up your score was: " + score + ". Press the r button to restart, or ESC to quit."); 
      if (Input.GetKeyDown("r")) 
       Application.LoadLevel("Testing Grounds"); 
      if (Input.GetKey("escape")) 
       Application.Quit(); 
     } 
    } 
} 

错误:

NullReferenceException: Object reference not set to an instance of an object CountDownTimer.Update() (at Assets/Scripts/CountDownTimer.cs:16)

我想不出什么可能会造成这个错误。据我了解,这是试图告诉我,一些东西不存在,但我想不出可能是什么。 “score = GetComponent()。score;”正在访问包含分数值的另一个脚本,以便当计时器用完时它会告知玩家他们的分数,并让他们选择退出或重新开始游戏。

此外,如果它帮助这里是其它脚本...

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 

公共类TriggerZone:MonoBehaviour {

public Text MyText; 
public int score; 

// Use this for initialization 
void Start() { 

    MyText.text = ""; 

} 


// Update is called once per frame 
void Update() { 

    MyText.text = "$" + score; 

} 

void OnTriggerEnter(Collider coll) { 

    if (coll.gameObject.HasTag ("ValueLevel1")) 
     score = score + 5; 

    if (coll.gameObject.HasTag ("ValueLevel2")) 
     score = score + 25; 

    if (coll.gameObject.HasTag ("ValueLevel3")) 
     score = score + 50; 

    if (coll.gameObject.HasTag ("ValueLevel4")) 
     score = score + 100; 

    if (coll.CompareTag ("Pickable")) { 
     coll.gameObject.SetActive(false); 
    } 

} 

}

+0

这听起来像'GetComponent ()'是'返回null',而是因为你没有足够的发布代码为我们重现您的问题,我们不能肯定。 – Enigmativity

+0

你还需要什么?另一个脚本? – Sie

回答

0

嗯,我觉得自己很蠢,但多亏了Reddit我终于找到了问题。原来,我把我的脚本附加到我的FPS控制器,这是造成null问题。

0
GetComponent<TriggerZone>() 

似乎返回null和您正尝试访问.score关闭null。您的解决方案是检查null的回报,并采取适当的措施或确保呼叫永不返回null

+0

这就是我迷失的地方。我使用“score = GetComponent ().score;”从TriggerZone获取分数值。没有它,它不起作用。所以它不能返回null。如果我没有意义,我对C#相对陌生,所以很抱歉。 – Sie

+0

检查两个脚本(即TriggerZone和CountDownTimer)是否连接到同一个GameObject。根据你的脚本,他们必须是。如果你需要它们在单独的游戏对象上,那么改变GetComponent行来读取类似score = REFERENCE_TO_OTHER_GO.GetComponent ().score; –

+0

它们都连接到同一个GameObject(如你所说的那样)。我不知道为什么这不起作用,因为游戏似乎运行良好。我很想尝试开发,但我宁愿先挤压这个bug。 – Sie

0

不论出于什么原因,你可能会错过你的CountDownTimer实例的TriggerZone脚本/组件,请尝试以下属性:

[RequireComponent (typeof (TriggerZone))] //Try add this 
public class CountDownTimer : MonoBehaviour { 
... 
} 

这RequireComponent属性可以让你自动添加所需的组件作为一个依赖。下面是Unity3D官方文档:

When you add a script which uses RequireComponent, the required component will automatically be added to the game object. This is useful to avoid setup errors. For example a script might require that a rigid body is always added to the same game object. Using RequireComponent this will be done automatically, thus you can never get the setup wrong.

BTW:以下调用应该不会在更新()调用,最好在开始()或甲戌():

score = GetComponent<TriggerZone>().score; 
+0

即使添加了RequireComponent事件,也是如此。错误刚刚移动到第12行(在Start()中)。 “NullReferenceException:未将对象引用设置为对象CountDownTimer.Start()(在Assets/Scripts/CountDownTimer中)的实例。cs:12)“ – Sie

+0

哪一行是12?你能否在你的文章中添加注释以显示第12行? – David

+0

以下是新代码:https://ideone.com/qQAipj。 – Sie