using UnityEngine;
using System.Collections;
public class Gamemanager : MonoBehaviour {
public GameObject AttackButton; //reference to attack button game object
public GameObject RocketGO; // reference to rocket game object
public GameObject EnemySpawner;//reference to meteorite spawner gameobject
public GameObject navbuttons;
public GameObject Power1SpawnerGO; // reference to power1 spawner
public GameObject Enemy1bluespawner;
// Use this for initialization
public enum GamemanagerState
{
Opening ,
Gameplay ,
Gameover,
}
GamemanagerState GMState;
void Start ()
{
GMState = GamemanagerState.Opening;
}
void UpdateGameManagerState()
{
switch(GMState)
{
case GamemanagerState.Opening:
//set attack button to true
AttackButton.SetActive(true);
//hide game over
//set navigational buttons to false
navbuttons.SetActive(false);
break;
case GamemanagerState.Gameplay:
//set attack button false
AttackButton.SetActive(false);
//set navbuttons to true
navbuttons.SetActive(true);
//set playership to active
RocketGO.GetComponent<Rocketdamage>().Init();
//Start EnemySpawner
EnemySpawner.GetComponent<meteoritespawner>().startEnemySpawner();
Enemy1bluespawner.GetComponent<Enemy1Spawner>().startEnemy1blueSpawner(); // Enemy blue spawner
//Active Rocket bullete fire
RocketGO.GetComponent<Rocketshooting>().startBulleteFire();
//Activate Enemyblue1 bulletefire
Enemy1bluespawner.GetComponent<Enemyblue1shooting>().startEBulleteFire();
//Active power1 spawner
Power1SpawnerGO.GetComponent<PowerSpawner>().startPower1Spawner();
break;
case GamemanagerState.Gameover:
//stop enemy spawner
EnemySpawner.GetComponent<meteoritespawner>().StopEnemySpawning();
Enemy1bluespawner.GetComponent<Enemy1Spawner>().StopEnemy1blueSpawning();
//DeActive Rocket bullete fire
RocketGO.GetComponent<Rocketshooting>().StopBulleteFire();
//Stop power1 spawning
Power1SpawnerGO.GetComponent<PowerSpawner>().StopPower1Spawning();
//Deactive Enemyblue1 Bullete fire
Enemy1bluespawner.GetComponent<Enemyblue1shooting>().StopEBulleteFire();
//display gameover
//set game state to opening state after 8 sec
Invoke("changeToOpeningState" , 8f);
break;
}
}
public void setGameManagerState(GamemanagerState state)
{
GMState = state;
UpdateGameManagerState ();
}
//call this function when player call attack button
public void Startpaly()
{
GMState = GamemanagerState.Gameplay;
UpdateGameManagerState ();
}
public void changeToOpeningState()
{
setGameManagerState (GamemanagerState.Opening);
}
}
This is my script.......and everything is running fine! But the line number 67 and 89 i.e.
//line no 67
Enemy1bluespawner.GetComponent<Enemyblue1shooting>().startEBulleteFire();
and:
//line no 89
Enemy1bluespawner.GetComponent<Enemyblue1shooting>().StopEBulleteFire();
under UpdateGameManagerState() function.
are giving me NullreferenceException
.
After so much of thinking, I have come up with this issue that may have been occurring!
Here
Enemy1bluespawner
is object(with which Spawning script is attached) to spawn enemy....andEnemyprefab
is attached toEnemy1bluespawner
...but however, I am able to call all the scripts/methods of Enemy PREFAB(Without direct reference of Enemy prefab - I don't know why?).Everything working fine but line number 67 and 89 is giving me
NullreferenceException
.
Scenario - I have empty object - Enemy1bluespawner
with which Enemyspawning
script is attached - which spawn the enemy at particular interval of time.........
So, when I am calling this function :
Enemy1bluespawner.GetComponent<Enemyblue1shooting>().startEBulleteFire();
To start bullete fire of the enemy but now enemy is not on the screen it will come after 5 seconds
The enemy is not on the screen it comes after 5 seconds - so may be that's why Unity is throwing NullreferenceException
(at runtime)
Questions:
How am I able to access Enemy prefab scripts/methods without direct reference thru
Enemy1bluespawn
Object?Solution to remove
NullreferenceException
. When enemy is not on screen and I am calling methoda related to it.
I hope you guys are understanding it. Any little help will be great. Thank you! :) :)