0

FindGameObjectWithTag is not accessing the script to initialize the variable I am using to store the script.

I have script checking if a power up is active and another setting the boolean variable that activates a power up on the collision. Through debugging I know the collision is working but for some reason I am getting the error above.

The game object tagged as "Enemy" is instantiated to the screen via code but I made sure to only perform the collision that activates the power up AFTER the object was instantiated in the game.

The power up check script is as follows:

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;
    }
}

The script performing the activation is as follows:

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);

        }
    }


}
  • 1
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jun 15 '15 at 19:23
  • But in the code I am getting the component script off of the game object, which should be keeping the variable from being null. That is the main source of my confusion, that the correct syntax of getting a component isn't getting the component for some reason. Thank you for the link though. – user3397025 Jun 15 '15 at 19:26
  • Use the debugger to single-step and learn which object is `null`. – John Saunders Jun 15 '15 at 19:39
  • I know that the burst variable is null however I don't see why it would be since I used GetComponent() to initialize it, which in Unity is the way to set the burst variable to a script attached to the game object tagged "Enemy". – user3397025 Jun 15 '15 at 19:42
  • I decided to instead go about the problem in a different way, directly instantiating the object on a condition instead of accessing the script on the game object. Thank you. – user3397025 Jun 15 '15 at 20:13

0 Answers0