Unity3d &引用;NullReferenceException";错误
我想在我的比赛中加入一个得分系统。然而,每当我玩的时候,我就会犯这个错误,当小行星与玩家或子弹碰撞时,它们不会再破坏 我的错误消息如下: NullReferenceException:对象引用未设置为对象的实例 DestroyByContact.OnTriggerEnter2D(UnityEngine.Collidater2D其他)(位于Assets/Scripts/DestroyByContact.cs:47) 我应该注意到,所有的游戏对象都有正确的标签 还有一些代码:Unity3d &引用;NullReferenceException";错误,unity3d,nullreferenceexception,unity5,Unity3d,Nullreferenceexception,Unity5,我想在我的比赛中加入一个得分系统。然而,每当我玩的时候,我就会犯这个错误,当小行星与玩家或子弹碰撞时,它们不会再破坏 我的错误消息如下: NullReferenceException:对象引用未设置为对象的实例 DestroyByContact.OnTriggerEnter2D(UnityEngine.Collidater2D其他)(位于Assets/Scripts/DestroyByContact.cs:47) 我应该注意到,所有的游戏对象都有正确的标签 还有一些代码: using Unity
using UnityEngine;
using System.Collections;
public class DestroyByContact : MonoBehaviour {
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
private GameController gameController;
void start () {
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
if (gameControllerObject != null) {
gameController = gameControllerObject.GetComponent <GameController> ();
}
if (gameController == null)
{
Debug.Log ("Cannot find 'GameController' script");
}
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Boundary") {
return;
}
Instantiate(explosion, transform.position, transform.rotation);
if (other.tag == "Player") {
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
}
gameController.AddScore (scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameController : MonoBehaviour {
public GameObject[] asteroids;
public Vector3 spawnValues;
public int asteroidCount;
public float spawnWait;
public float startWait;
public float waveWait;
public GUIText scoreText;
private int score;
void Start () {
score = 0;
UpdateScore ();
StartCoroutine (spawnWaves ());
}
IEnumerator spawnWaves () {
yield return new WaitForSeconds (startWait);
while (asteroidCount > 0) {
for (int i = 0; i < asteroidCount; i++) {
GameObject asteroid = asteroids[Random.Range(0, asteroids.Length)];
Vector3 spawnPosition = new Vector3 (spawnValues.x, Random.Range (-spawnValues.y, spawnValues.y), spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (asteroid, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
if (asteroidCount <= 95) {
asteroidCount += 5;
}
}
}
public void AddScore (int newScoreValue) {
score += newScoreValue;
UpdateScore ();
}
void UpdateScore () {
scoreText.text = "Score:" + score;
}
}
使用UnityEngine;
使用系统集合;
公共类:单行为{
公开游戏对象爆炸;
公开游戏对象玩家爆炸;
公共价值;
私人游戏控制器;
无效开始(){
GameObject gameControllerObject=GameObject.FindWithTag(“GameController”);
if(gameControllerObject!=null){
gameController=gameControllerObject.GetComponent();
}
如果(gameController==null)
{
Log(“找不到'GameController'脚本”);
}
}
无效OnTiggerEnter2D(碰撞的R2D其他){
if(other.tag==“Boundary”){
回来
}
实例化(爆炸、变换.位置、变换.旋转);
如果(other.tag==“玩家”){
实例化(playerExplosion,other.transform.position,other.transform.rotation);
}
gameController.AddScore(scoreValue);
销毁(其他游戏对象);
摧毁(游戏对象);
}
}
使用UnityEngine;
使用系统集合;
使用UnityEngine.UI;
公共类游戏控制器:单行为{
公众游戏对象[]小行星;
公共向量值;
公众人数;
公众等待;
公共浮动startWait;
公众等待;
公共文本;
个人智力得分;
无效开始(){
得分=0;
UpdateScore();
start例程(spawnWaves());
}
IEnumerator生成波(){
返回新的WaitForSeconds(startWait);
而(计数>0){
对于(int i=0;i如果(asteroidCount我想说更好的办法是在实例化控制器内部后设置gameController
。如果我理解正确,您的gameController
会产生从预制/预定义集合附加的DestroyOnContact
小行星?
如果是这样,考虑这样做:
GameController
var go = Instantiate (asteroid, spawnPosition, spawnRotation) as GameObject;
if (destroyable != null)
{
var destroyable = go.GetComponent<DestroyByContact>();
if (destroyable == null)
{
//Something else got instantiated, destroy it as we don't need it here
Destroy(destroyable);
}
else
{
destroyable.gameController = this;
}
}
哪一行是DestroyByContact.cs
的第47行?您认为那里的变量是如何设置的?一个NullReferenceException
通常很容易调试:您做了什么来试图理解这个问题?第47行是:gameController.AddScore(scoreValue).老实说,我是新手,我正在使用一些教程来尝试和学习。我不确定如何调试它。我知道的一件事是,如果我将第47行放在销毁函数下面,它仍然工作,但不添加分数(我想这是因为游戏对象不再用于运行代码).Mark,而不是使用FindWithTag从层次结构中分配GameController。如果您不知道如何分配,请重试与我们共享unity3d的屏幕截图。我正在尝试引用附加到我的game controller game Object的game controller脚本,以便在小行星被摧毁时告诉它增加玩家分数。没有任何东西可以阻止你实际上,通过适当的依赖注入,你应该从外部注入GameController(不幸的是,构造函数不是MonoBehavior的有效方法)在我的例子中,我没有从DestroyOnContact内部找到它。我不完全确定它是如何工作的。我尝试将它放入我的代码中,但没有任何效果。
//just change accessor from private to public
public GameController gameController;
//Get rid of stuff that would set gameController in Start()
//Also check if gameController != null before you call .AddScore()