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 为什么我按照电路图做出的仿真和实物都不能使用
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web