qq_35219692 2016-06-24 06:38 采纳率: 54.5%
浏览 1823

unity3d c#调用脚本报错

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);。也不知道为什恶魔,求解

  • 写回答

3条回答

  • herozhangbz 2016-06-24 06:44
    关注

    你应该这么写吧

         GameObject gamecontrolobject = GameObject.FindWithTag("GameController");
        if (gamecontrolobject != null)
        {
            game = gamecontrolobject.GetComponent<Gamecontrol>();
            game.addScore(score1);          
        }
        else
            Debug.Log("can't find 'Gamecontrol' scipt");
    
    
    
    

    你都不能确定game得到有效赋值,你就给加力明显不对的

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?