繁体   English   中英

gameObject.GetComponent返回null

[英]gameObject.GetComponent returning null

我有一个问题。

在我的一个脚本中,当我尝试获取组件时,它在控制台中返回此错误,并返回null:“ NullReferenceException:在需要对象实例的位置找到了null值。”

我不知道为什么,因为在其他文件中,我使用的过程效果很好。

这是我的代码流:第一个-GameController.cs

public class GameController : MonoBehaviour {

    private SpawningPositions spawningPositions; //IT WORKS

    private void Awake()
    {
        spawningPositions = GetComponent<SpawningPositions>(); //IT WORKS
    }

    void Start()
    {
        if (enemyWaveTypes.Length != 0 && wavesNumberEnemy.Length != 0)
        {
            StartCoroutine(SpawnShips());
        }
    }

    IEnumerator SpawnShips()
    {
        while (true)
        {
            for (int i = 0; i < wavesNumberEnemy[waveCounter]; i++)
            {
                spawningPositions.SpawnRandom(enemyShip); //IT WORKS // NEXT SCRIPT
            }
            waveCounter++;
            yield return new WaitForSeconds(waveTimingRatio);
        }
    }
}

第二个-SpawningPositions.cs

public class SpawningPositions : MonoBehaviour {

    GeoData geoData; //IT WORKS

    private void Awake()
    {
        geoData = GetComponent<GeoData>(); //IT WORKS
    }

    public void SpawnRandom(Transform enemyShip)
    {
        Vector3 spawnPosition;
        Tuple<int, float> tuple = geoData.GetRandomOuterBoundary();  //IT WORKS (i've got a custom class for Tuples)
        int randomSpawn = tuple.Item1;
        float randomPosition = tuple.Item2;
        spawnPosition = geoData.GetRandomPointIn(tuple);
        Quaternion spawnRotation = Quaternion.identity;
        var myEnemy = Instantiate(enemyShip, spawnPosition, spawnRotation);
        myEnemy.gameObject.AddComponent<FixedPointAndGo>(); // NEXT SCRIPT
    }

}

最后一个是我遇到问题的地方-FixedPointAndGo.cs

public class FixedPointAndGo : MonoBehaviour {

    GeoData geoData; // DOESN'T WORK!!!

    private void Awake()
    {
        geoData = gameObject.GetComponent<GeoData>();  // DOESN'T WORK!!! return NULL
    }

    private void Start()
    {       
        endPos = new Vector3(
            Random.Range(
                (geoData.horizontalInLimits.x - 2), // DOESN'T WORK!!!
                (geoData.horizontalInLimits.y - 2) // DOESN'T WORK!!!
            ),
            0,
            Random.Range(geoData.verticalInLimits.x, geoData.verticalInLimits.y) // DOESN'T WORK!!!
        );
    }
}

为什么当我尝试在第二个脚本而不是第三个脚本中添加组件GeoData时起作用? 我不明白。

我试图在此论坛和文档中搜索解决方案,但尚未找到任何东西。

提前致谢

geoData = gameObject.GetComponent<GeoData>();  // DOESN'T WORK!!! return NULL

注意变量“ gameObject ”。 这是指此脚本( FixedPointAndGo )附加到的FixedPointAndGo

这意味着GeoData脚本附加到您的FixedPointAndGo脚本附加到的FixedPointAndGogameObject.GetComponent<GeoData>() 返回null

GeoData附加到您的FixedPointAndGo脚本附加到的相同FixedPointAndGo


如果将GeoData附加到另一个GeoData ,并且您想访问它,则在访问附加到它的GeoData组件之前,先找到该GeoData

GameObject obj = GameObject.Find("NameOfGameObjectGeoDataIsAttachedTo"); 
geoData = obj.GetComponent<GeoData>();

最后,如果GeoData脚本附加到相同的FixedPointAndGo脚本上,但是您仍为null则错误地将另一个FixedPointAndGo脚本附加到一个空的FixedPointAndGo或另一个未附加GeoData脚本的GeoData

找到该FixedPointAndGoFixedPointAndGo删除FixedPointAndGo脚本。 您可以选择“ FixedPointAndGo脚本,然后转到“ 资产 ---> 在场景中查找引用”并删除脚本来完成此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM