using UnityEngine;
using System.Collections;
public class Destorybycontact : MonoBehaviour {
public GameObject explosion;
public GameObject playerexplosion;
public int score1;
private Gamecontrol game;
void start()
{
GameObject gamecontrolobject = GameObject.FindWithTag("GameController");
if (gamecontrolobject != null)
{
game = gamecontrolobject.GetComponent<Gamecontrol>();
}
if(gamecontrolobject == null)
{
Debug.Log("can't find 'Gamecontrol' scipt");
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "boundary")
{ return; }
Instantiate(explosion, transform.position, transform.rotation);
if (other.tag == "Player")
{
Instantiate(playerexplosion, other.transform.position, other.transform.rotation);
}
game.addScore(score1);
Destroy(other.gameObject);
Destroy(gameObject);
}
}
报错NullReferenceException: Object reference not set to an instance of an object。显示这个是空值game.addScore(score1);。也不知道为什恶魔,求解
你应该这么写吧
GameObjectgamecontrolobject=GameObject.FindWithTag("GameController");if(gamecontrolobject!=null){game=gamecontrolobject.GetComponent<Gamecontrol>();game.addScore(score1);}elseDebug.Log("can'tfind'Gamecontrol'scipt");
你都不能确定game得到有效赋值,你就给加力明显不对的
我是渣渣你试试下面这个
voidstart(){}voidOnTriggerEnter(Colliderother){if(other.tag=="boundary"){return;}Instantiate(explosion,transform.position,transform.rotation);if(other.tag=="Player"){Instantiate(playerexplosion,other.transform.position,other.transform.rotation);}GameObjectgamecontrolobject=GameObject.FindWithTag("GameController");if(gamecontrolobject!=null){game=gamecontrolobject.GetComponent<Gamecontrol>();game.addScore(score1);}elseDebug.Log("can'tfind'Gamecontrol'scipt");Destroy(other.gameObject);Destroy(gameObject);}
空指针,找一下报错的地方的代码,看一下什么东西没找到,这种小问题,好解决
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。