首页 > 解决方案 > MissingReferenceException:“GameObject”类型的对象已被销毁,但您仍在尝试访问它

问题描述

我是团结的初学者,我试图制作简单的球类游戏,其中我生成随机物体,玩家必须保存球以免击中物体,我也在球通过这些物体后摧毁物体,但我得到了这个错误。

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, UnityEngine.Vector3 pos, UnityEngine.Quaternion rot) (at <9baebf9af86541678fd15bfdbf5f26eb>:0)
UnityEngine.Object.Instantiate (UnityEngine.Object original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at <9baebf9af86541678fd15bfdbf5f26eb>:0)
UnityEngine.Object.Instantiate[T] (T original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at <9baebf9af86541678fd15bfdbf5f26eb>:0)
ObstacleSpawner.spawner () (at Assets/scripts/ObstacleSpawner.cs:26)

myObstacleSpawner 脚本是

public class ObstacleSpawner : MonoBehaviour
{
    public GameObject[] obstacle;
// Start is called before the first frame update
void Start()
{ 
        InvokeRepeating("spawner", 0.5f, 0.5f);
    
    
}

// Update is called once per frame
void Update()
{
    transform.Translate(Vector3.forward * 50 * Time.deltaTime);
    
}

public void spawner()
{
    Instantiate(obstacle[Random.Range(0, 2)], new Vector3(Random.Range(-3f,3f), transform.position.y, transform.position.z), Quaternion.identity);

}

}

我的毁灭者脚本是

public class Destroyer : MonoBehaviour
{
// Start is called before the first frame update
public float speedDestroyer = 10f;
private void OnTriggerEnter(Collider other)
{
    if(other.gameObject.tag == "enemy")
    {
        Destroy(other.gameObject);
    }
}

void Start()
{
    
}

// Update is called once per frame
void Update()
{
    transform.Translate(Vector3.forward * speedDestroyer * Time.deltaTime);
       
}
}

标签: c#unity3d

解决方案


推荐阅读