Мой сценарий ...Почему я продолжаю получать эту ошибку 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);
}
}
}
Звучит так: 'GetComponent()' возвращает 'null', но мы не можем быть уверены, потому что вы не разместили достаточно кода, чтобы мы могли воспроизвести вашу проблему. –
Enigmativity
Что еще вам нужно? Другой скрипт? – Sie