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