使用带标记的FindGameObject访问游戏对象上的脚本

时间:2015-06-15 19:17:45

标签: c# unity3d nullreferenceexception

FindGameObjectWithTag没有访问脚本来初始化我用来存储脚本的变量。

我有脚本检查是否有电源处于活动状态,另一个设置布尔变量来激活碰撞上电。通过调试我知道碰撞正在工作但由于某种原因我得到上面的错误。

标记为“Enemy”的游戏对象通过代码实例化到屏幕,但我确保只执行在游戏中实例化对象后激活加电的碰撞。

加电检查脚本如下:

using UnityEngine;
using System.Collections;

public class PowerUp_Check : MonoBehaviour {

    //burst script on enemy
    //time on main camera
    //shield on main camera
    //score on main camera
    //clear screen on main camera (for now)

    //state variables
    public static bool pu_burst;
    public static bool pu_time;
    public static bool pu_shield;
    public static bool pu_score;
    public static bool pu_clearScreen;

    //references to scripts
     private Burst burst;
     private IncreaseTime increaseTime;
     private Shield shield;
     private IncreaseScore increaseScore;
     private ClearScreen clearScreen;

    // Use this for initialization
    void Awake () 
    {
        //none of the power ups start out as active
        pu_burst = false;
        pu_time = false;
        pu_shield = false;
        pu_score = false;
        pu_clearScreen = false;

        burst = GameObject.FindGameObjectWithTag ("Enemy").GetComponent<Burst> ();
        increaseTime = GameObject.FindGameObjectWithTag ("Camera").GetComponent<IncreaseTime> ();
        shield = GameObject.FindGameObjectWithTag ("Camera").GetComponent<Shield> ();
        increaseScore = GameObject.FindGameObjectWithTag ("Camera").GetComponent<IncreaseScore> ();
        clearScreen = GameObject.FindGameObjectWithTag ("Camera").GetComponent<ClearScreen> ();
    }

    // Update is called once per frame
    void Update () {

        if (pu_burst) {
            burst.enabled = true;
            print ("Burst active");


        }
        else
            burst.enabled = false;
    //----------------------------------------//
        if (pu_time)
            increaseTime.enabled = true;
        else
            increaseTime.enabled = false;
    //----------------------------------------//
        if (pu_shield)
            shield.enabled = true;
        else
            shield.enabled = false;
    //----------------------------------------//
        if (pu_score)
            increaseScore.enabled = true;
        else
            increaseScore.enabled  = false;
    //----------------------------------------//
        if (pu_clearScreen)
            clearScreen.enabled = true;
        else
            shield.enabled = false;
    }
}

执行激活的脚本如下:

using UnityEngine;
using System.Collections;

public class MissileDestroy : MonoBehaviour {

    //This script is to be attached to the missile
    //If the missile hits an enemy, that enemy is to be destroyed

    public GameObject blueFlame;


    // Update is called once per frame
    void Update () 
    {

    }

    void OnCollisionEnter(Collision other)
    {
        GameObject flame;

        if (other.gameObject.tag == "Enemy") {

            //show a blue explosion after killing enemy
            flame = (GameObject)Instantiate (blueFlame, transform.position, Quaternion.Euler (0,0,0));

            //the enemy is no longer alive
            EnemyHealth.alive = false;

            //destroy the enemy
            Destroy (other.transform.parent.gameObject);

            //destroy the missile
            Destroy (gameObject);

            //add 100 points to the score
            GameState.score += 100;


        }

        else if (other.gameObject.tag == "isBurst") {
            print ("Collision entered");

            PowerUp_Check.pu_burst = true;

            Destroy (other.gameObject);

        }
    }


}

0 个答案:

没有答案