为什么我'我得到空异常时,试图GetComponent

本文关键字:异常 试图 GetComponent 为什么 | 更新日期: 2023-09-27 18:13:18

两个脚本在层次结构中都附加到相同的空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);
    }
}

为什么我'我得到空异常时,试图GetComponent

这是一种可能性,你的衍生对象没有附加的组件,你试图访问。因此,在使用它之前检查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;
相关文章: