为什么我在尝试GetComponent时遇到null异常?

时间:2016-10-17 10:21:37

标签: c# unity3d

两个脚本都附加到层次结构中相同的空GameObject。 首先附加SpawnObjects脚本然后附加MoveObjects。

这是带有例外的脚本。 例外是在线:

mover.minXPos = minXPos;

异常消息:

NullReferenceException: Object reference not set to an instance of an object
SpawnObjects.RandomSpawn () (at Assets/MyScripts/SpawnObjects.cs:28)
SpawnObjects.Start () (at Assets/MyScripts/SpawnObjects.cs:18)

我的代码:

using UnityEngine;
using System.Collections;

public class SpawnObjects : MonoBehaviour {

    public GameObject PrefabToSpawn;
    public int MaximumObjects = 100;
    public int minXPos = -1000;
    public int maxXPos = 1000;
    public int minYPos = 50;
    public int maxYPos = 150;
    public int minZPos = -1000;
    public int maxZPos = 1000;

    // Use this for initialization
    void Start () {

        RandomSpawn();
    }

    private void RandomSpawn()
    {
        for (int i = 0; i < MaximumObjects; i++)
        {
            Vector3 spawnLocation = new Vector3(Random.Range(minXPos, maxXPos), Random.Range(minYPos, maxYPos), Random.Range(minZPos, maxZPos));
            GameObject spawned = (GameObject)Instantiate(PrefabToSpawn, spawnLocation, Quaternion.identity);
            MoveObjects mover = spawned.GetComponent<MoveObjects>();
            mover.minXPos = minXPos;
            mover.maxXPos = maxXPos;
            mover.minYPos = minYPos;
            mover.maxYPos = maxYPos;
            mover.minZPos = minZPos;
            mover.maxZPos = maxZPos;
        }
    }
}

这是MoveObjects的脚本

using UnityEngine;
using System.Collections;

public class MoveObjects : MonoBehaviour {

    public int minXPos = -1000;
    public int maxXPos = 1000;
    public int minYPos = 50;
    public int maxYPos = 150;
    public int minZPos = -1000;
    public int maxZPos = 1000;
    public float speed = 30;

    private Vector3 destinationLocation;

    private float midX;
    private float midY;
    private float midZ;

    void Start()
    {
        midX = (minXPos + maxXPos) / 2;
        midY = (minYPos + maxYPos) / 2;
        midZ = (minYPos + maxYPos) / 2;
        GenerateNewDestinationPoint();
    }

    void Update()
    {
        Move();
        if (ArrivedAtLocation())
            GenerateNewDestinationPoint();
    }

    private void Move()
    {
        transform.LookAt(destinationLocation);
        transform.Translate(transform.forward * speed * Time.deltaTime);
    }

    private bool ArrivedAtLocation()
    {
        return (Vector3.Distance(transform.position, destinationLocation) < 1);
    }

    private void GenerateNewDestinationPoint()
    {
        float newX = (transform.position.x < midX) ? Random.Range(midX, maxXPos) : Random.Range(minXPos, midX);
        float newY = (transform.position.y < midY) ? Random.Range(midY, maxYPos) : Random.Range(minYPos, midY);
        float newZ = (transform.position.z < midZ) ? Random.Range(midZ, maxZPos) : Random.Range(minZPos, midZ);

        destinationLocation = new Vector3(newX, newY, newZ);
    }
}

1 个答案:

答案 0 :(得分:1)

您生成的对象可能没有附加您尝试访问的组件。因此,在使用之前检查null始终是安全的。

    MoveObjects mover = spawned.GetComponent<MoveObjects>();
    if(mover == null)
    {
        // your prefab doesn't have the component attached. maybe add it.
        mover = spawned.AddComponent<MoveObject>();
    }
    mover.minXPos = minXPos;
    mover.maxXPos = maxXPos;
    mover.minYPos = minYPos;
    mover.maxYPos = maxYPos;
    mover.minZPos = minZPos;
    mover.maxZPos = maxZPos;
相关问题