0
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....and Enemyprefab is attached to Enemy1bluespawner...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! :) :)

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Nitin Rathod
  • 159
  • 2
  • 15

2 Answers2

0

easiest but not the best is

if(Enemy1bluespawner.GetComponent<Enemyblue1shooting>()!=null)
Enemy1bluespawner.GetComponent<Enemyblue1shooting>().startEBulleteFire();
Milad Qasemi
  • 3,011
  • 3
  • 13
  • 17
0

You reference the Enemy1Spawner script, but never the Enemyblue1shooting. For what I'm understanding, your goal is to spawn enemies with the Enemy1Spawner script, and those spawned enemies are the objects that have the Enemyblue1shooting component (that will actually shoot).

If I'm right, you'll have to get the GameObject of the spawned enemy and then use the GetComponent method. What seems to be happening is a NullPointerException because the spawner doesn't have a Enemyblue1shooting component; the object that has it is it's child.

Try making a method in the Enemy1bluespawner script that returns the enemy at hand and, only then, using GetComponent<Enemyblue1shooting>() to get the right component. That'll do the trick.

Example:

public GameObject GetEnemy(int n)
{
    return enemies.get(n); // Gets the n enemy in your array
}

...

Enemy1bluespawner.GetComponent<Enemy1Spawner>().startEnemy1blueSpawner(); // Enemy blue spawner

GameObject enemy = Enemy1bluespawner.GetComponent<Enemy1Spawner>().GetEnemy(0);
enemy.GetComponent<Enemyblue1shooting>().startEBulleteFire();

Don't forget to do some null validation. The perfect scenario would be returning the script itself, not a GameObject, but this way it may be a bit easier to see.

That might be also your question about calling prefab's methods. You don't call a prefab's method; you call the method of the instantiated prefab (the spawned enemy).