NullReferenceException&使用方法没有直接参考

时间:2015-09-25 13:53:17

标签: c# unity3d

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即

//第67行

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

//第89行

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

在UpdateGameManagerState()函数下。

正在给我NullreferenceException

经过这么多思考,我已经提出了可能已经发生的这个问题!

  • 这里Enemy1bluespawner是对象(附有产生脚本的对象)来产生敌人.... Enemyprefab附加到Enemy1bluespawner ...但是,我能够调用Enemy PREFAB的所有脚本/方法(没有直接参考Enemy预制件 - 我不知道为什么?)。

  • 一切正常,但第67和89行给我NullreferenceException

场景 - 我有一个空对象 - Enemy1bluespawner,其上附有Enemyspawning脚本 - 在特定的时间间隔产生敌人........

所以,当我调用这个函数时:

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

要开始敌人的大胆射击,但现在敌人不在屏幕上,它将在5秒后到来

5秒后敌人不在屏幕上 - 所以可能是因为Unity投掷NullreferenceException(在运行时)

问题:

  • 如何通过Enemy1bluespawn对象直接引用来访问Enemy预制脚本/方法?

  • 删除NullreferenceException的解决方案。当敌人不在屏幕上时,我正在调用与之相关的方法。

我希望你们能理解它。任何一点帮助都会很棒。谢谢! :):)

2 个答案:

答案 0 :(得分:0)

最简单但不是最好的

<html>
<head>
<script>

window.onload = function() {
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
W = window.innerWidth,
H = window.innerHeight;


setInterval(draw, 33);

function draw() {
ctx.globalcompositeOperation = "source-over";
ctx.fillStyle = "black";
ctx.fillRect(0,0,W,H);
}
}

</script>
</head>

<body>
    <canvas id="canvas"></canvas>

答案 1 :(得分:0)

您引用Enemy1Spawner脚本,但不引用Enemyblue1shooting。对于我的理解,你的目标是使用Enemy1Spawner脚本产生敌人,那些产生的敌人是拥有Enemyblue1shooting组件的对象(实际拍摄)。

如果我没错,你必须得到产生的敌人的GameObject,然后使用GetComponent方法。似乎正在发生的是NullPointerException,因为产生者没有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,但这样可能会更容易看到。

这也可能是关于调用prefab方法的问题。你没有打电话给预制的方法;你调用实例化预制(生成的敌人)的方法。