Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# gameObject.GetComponent返回null_C#_Unity3d - Fatal编程技术网

C# gameObject.GetComponent返回null

C# gameObject.GetComponent返回null,c#,unity3d,C#,Unity3d,我有个问题 在我的一个脚本中,当我尝试获取组件时,它会在控制台中返回null,并出现以下错误:“NullReferenceException:在需要对象实例的位置找到null值。” 我不知道为什么,因为在其他文件中,我使用的过程运行良好 这里是我的代码流程:第一个一-GameController.cs public class GameController : MonoBehaviour { private SpawningPositions spawningPositions; //

我有个问题

在我的一个脚本中,当我尝试获取组件时,它会在控制台中返回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);
        }
    }
}
公共类游戏控制器:单行为{
private sprowingpositions sprowingpositions;//有效
私人空间
{
spawningPositions=GetComponent();//它可以工作
}
void Start()
{
if(enemyWaveTypes.Length!=0&&wavesNumberEnemy.Length!=0)
{
startcootine(SpawnShips());
}
}
IEnumerator产卵器()
{
while(true)
{
对于(int i=0;i
第二个-spawingpositions.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
    }

}
公共类生成位置:单行为{
GeoData GeoData;//它可以工作
私人空间
{
geoData=GetComponent();//它可以工作
}
公共空间(转换敌人身份)
{
矢量3产卵位置;
Tuple Tuple=geoData.GetRandomOuterBoundary();//它可以工作(我有一个用于Tuple的自定义类)
int randompawn=tuple.Item1;
float randomPosition=tuple.Item2;
spawnPosition=geoData.GetRandomPointIn(元组);
四元数=四元数.identity;
var myEnemy=实例化(Eneyship、spawnPosition、spawnRotation);
myEnemy.gameObject.AddComponent();//下一个脚本
}
}
最后一个,我有问题-fixedpoint和go.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!!!
        );
    }
}
公共类FixedPointAndGo:monobhavior{
GeoData GeoData;//不工作!!!
私人空间
{
geoData=gameObject.GetComponent();//不工作!!!返回NULL
}
私有void Start()
{       
endPos=新矢量3(
随机范围(
(geoData.horizontalInLimits.x-2),//不起作用!!!
(geoData.horizontalInLimits.y-2)//不起作用!!!
),
0,
Range(geoData.verticalimits.x,geoData.verticalimits.y)//不起作用!!!
);
}
}
为什么我尝试在第二个脚本中添加组件
GeoData
,而在第三个脚本中却没有?我不明白

我试图在这个论坛和文档中搜索解决方案,但我还没有找到任何东西

提前谢谢

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

最后,如果
GeoData
脚本已附加到同一游戏对象
fixedpoint和go
脚本已附加到,但仍然得到
null
,则您错误地将另一个
fixedpoint和go
脚本附加到一个空游戏对象或另一个没有
GeoData
的游戏对象附加到它的脚本

GameObject obj = GameObject.Find("NameOfGameObjectGeoDataIsAttachedTo"); 
geoData = obj.GetComponent<GeoData>();
找到该游戏对象并从中删除
FixedPointAndGo
脚本。您可以通过选择
fixedpoint和go
脚本,然后转到“资源”->在场景中查找引用并删除脚本来完成此操作