MissingReferenceException:“GameObject”类型的对象已被销毁,但您仍在尝试访问它

时间:2021-04-29 14:47:53

标签: c# unity3d

我是统一的初学者,我正在尝试制作简单的球类游戏,其中我生成随机对象,玩家必须保存球以免击中物体,我也在球通过这些物体后销毁物体,但我收到此错误。< /p>

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);
       
}
}

0 个答案:

没有答案
相关问题