unity3d学习——问题遗留(未解决)

来源:互联网 发布:python re.match 编辑:程序博客网 时间:2024/05/20 11:53

     游戏本体并没有完成,所以这篇博客仅供日后个人参考

     没有完成的主要原因是教程代码与现用代码出现不符,当然这其实没什么影响,因为已经找到了可以使用的方法

     找到了是找到了,那么问题还是出现报错,应该是出在两部分代码的链接上

下面是两个部分的代码:

第一部分——消灭模块

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Destory : MonoBehaviour {    public GameObject explosion;    public GameObject playerExplosion;    public Done_GameController gameController;    public int scoreValue;    void start()    {        GameObject gameControllerObject = GameObject.FindGameObjectWithTag("GameController");        if (gameControllerObject != null)        {            gameController = gameControllerObject.GetComponent<Done_GameController>();        }        if (gameControllerObject == null)        {            Debug.Log("Can not find GameContraller Scripts");        }    }    void OnTriggerEnter(Collider other)    {        if (other.tag == "Wall"||other.tag == "Enemy")//使物体不会因碰撞被消除        {            return;        }        if (other.tag == "Player")//玩家爆炸效果        {            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);            gameController.GameOver();        }        if (explosion != null)//行星爆炸效果        {            Instantiate(explosion, other.transform.position, other.transform.rotation);        }        //玩家以及行星因碰撞消失        //gameController.AddScore(scoreValue);        Destroy(other.gameObject);        Destroy(this.gameObject);                   }}
然后是gameController

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class GameContraller : MonoBehaviour {    public GameObject[] hazards;//这里存放之前预设好的游戏物体小行星    public Vector3 spawnValues;//随即生成小行星的未知    public int hazardCount;//每一波生成的数量    public float spawnWait;//每一个生成的时间    public float startWait;//第一波等待的时间    public float waveWait;//每一波等待的时间    public Text scoreText;//得分    public Text gameOverText;//游戏结束    public Text restartText;//重新开始    private int score;    private bool gameOver;    private bool restart;        // Use this for initialization    void Start () {        gameOverText.text = "";        restartText.text = "";        gameOver = false;        restart = false;         score = 0;        UpdateScore();        StartCoroutine(SpawnWave());}    void Update()    {        if (restart)        {            if (Input.GetKeyDown(KeyCode.R))            {                Application.LoadLevel(Application.loadedLevel);            }        }    }   IEnumerator SpawnWave()    {        yield return new WaitForSeconds(startWait);        while (true)        {            for (int i = 0; i < hazardCount; i++)            {                //这一波代码很难理解。。。。                GameObject hazard = hazards[Random.Range(0, hazards.Length)];//从数组中选择一个物体                Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);//物体出现的位置                Instantiate(hazard, spawnPosition, Quaternion.identity);                yield return new WaitForSeconds(spawnWait);            }            yield return new WaitForSeconds(waveWait);            if (gameOver)            {                restartText.text = "Press 'R' for Restart";                restart = true;                break;            }        }    }    //积分增加    public void AddScore(int newScoreValue)    {        score += newScoreValue;        UpdateScore();     }    //更新积分显示    void UpdateScore()    {        scoreText.text = "Score:" + score;    }    public void GameOver()    {        gameOverText.text = "Game Over!!!";        gameOver = true;    }}


接下来是报错原因::NullReferenceException: Object reference not set to an instance of an object
Destory.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Sprites/Destory.cs:37)


这段代码使用后没有计分(当然是因为我把计分功能注释掉了)没有重新开始,没有死亡,即玩家不会被消灭


以上问题预后解决,完成后发文

原创粉丝点击