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);
}
}
這是我的腳本.......一切都運行良好!但行號67和89即NullReferenceException&使用方法沒有直接引用
//無線67
Enemy1bluespawner.GetComponent<Enemyblue1shooting>().startEBulleteFire();
和:
//無線89
Enemy1bluespawner.GetComponent<Enemyblue1shooting>().StopEBulleteFire();
下UpdateGameManagerState()函數。
給我NullreferenceException
。
經過這麼多的思考,我想出了這個問題可能已經發生!
這裏
Enemy1bluespawner
爲對象(與產卵腳本附後)產卵敵人....和Enemyprefab
連接到Enemy1bluespawner
...但但是,我能叫敵人的所有腳本/方法PREFAB(沒有直接提及敵人預製 - 我不知道爲什麼?)。一切正常,但行號67和89給我
NullreferenceException
。
方案 - 我有空對象 - Enemy1bluespawner
與Enemyspawning
腳本連接 - 這在產卵時間間隔尤其敵人.........
所以,當我我調用這個函數:
Enemy1bluespawner.GetComponent<Enemyblue1shooting>().startEBulleteFire();
要啓動的敵人bullete火,但現在的敵人不是在屏幕上它會在5秒後
的敵人不是說到5秒後在屏幕上 - 所以可能這就是爲什麼統一拋出NullreferenceException
(在運行時)
問題:
我怎麼能夠訪問敵人預製腳本/方法沒有直接引用通過
Enemy1bluespawn
對象?解決方案刪除
NullreferenceException
。當敵人不在屏幕上時,我正在調用與之相關的方法。
我希望你們能理解它。任何小小的幫助都會很棒。謝謝! :) :)
http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it –