2015-09-25 41 views
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); 
} 
} 

這是我的腳本.......一切都運行良好!但行號67和89即NullReferenceException&使用方法沒有直接引用

//無線67

Enemy1bluespawner.GetComponent<Enemyblue1shooting>().startEBulleteFire(); 

和:

//無線89

Enemy1bluespawner.GetComponent<Enemyblue1shooting>().StopEBulleteFire(); 

下UpdateGameManagerState()函數。

給我NullreferenceException

經過這麼多的思考,我想出了這個問題可能已經發生!

  • 這裏Enemy1bluespawner爲對象(與產卵腳本附後)產卵敵人....和Enemyprefab連接到Enemy1bluespawner ...但但是,我能叫敵人的所有腳本/方法PREFAB(沒有直接提及敵人預製 - 我不知道爲什麼?)。

  • 一切正常,但行號67和89給我NullreferenceException

方案 - 我有空對象 - Enemy1bluespawnerEnemyspawning腳本連接 - 這在產卵時間間隔尤其敵人.........

所以,當我我調用這個函數:

Enemy1bluespawner.GetComponent<Enemyblue1shooting>().startEBulleteFire(); 

要啓動的敵人bullete火,但現在的敵人不是在屏幕上它會在5秒後

的敵人不是說到5秒後在屏幕上 - 所以可能這就是爲什麼統一拋出NullreferenceException(在運行時)

問題:

  • 我怎麼能夠訪問敵人預製腳本/方法沒有直接引用通過Enemy1bluespawn對象?

  • 解決方案刪除NullreferenceException。當敵人不在屏幕上時,我正在調用與之相關的方法。

我希望你們能理解它。任何小小的幫助都會很棒。謝謝! :) :)

+0

http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it –

回答

0

您引用Enemy1Spawner腳本,但從來沒有Enemyblue1shooting。對於我的理解,你的目標是產生敵人與Enemy1Spawner腳本,而那些產生的敵人是具有Enemyblue1shooting組件(這將實際拍攝)的對象。

如果我是對的,你必須得到產生敵人的GameObject,然後使用GetComponent方法。似乎正在發生的是NullPointerException,因爲spawner沒有Enemyblue1shooting組件;擁有它的對象是它的孩子。

嘗試在Enemy1bluespawner腳本中製作一個方法,該方法返回手中的敵人,然後僅使用GetComponent<Enemyblue1shooting>()來獲取正確的組件。這將會訣竅。

例子:

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

不要忘記做一些空的驗證。完美的場景會返回腳本本身,而不是GameObject,但這樣看起來可能更容易一些。

這也可能是您調用預製方法的問題。你不要調用預製的方法;你調用實例化的預製(產生的敵人)的方法。

+0

我明白你的回答.... thanx幫助..... !! :) –

0

最簡單的,但不是最好的是

if(Enemy1bluespawner.GetComponent<Enemyblue1shooting>()!=null) 
Enemy1bluespawner.GetComponent<Enemyblue1shooting>().startEBulleteFire(); 
+0

它的工作...... Thanx幫助...: ) –

相關問題