【发布时间】:2021-06-26 19:27:07
【问题描述】:
MissingReferenceException:“GameObject”类型的对象已被销毁,但您仍在尝试访问它。 您的脚本应该检查它是否为空,或者您不应该销毁该对象。 ObjectPooler.GetPooledObject () (在 Assets/Scripts/ObjectPooler.cs:31) PlatformGenerator.Update () (在 Assets/Scripts/PlatformGenerator.cs:57)
PlatformGenerator
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformGenerator : MonoBehaviour
{
public GameObject thePlatform;
public Transform generationPoint;
public float distanceBetween;
private float platformWidth;
public float distanceBetweenMin;
public float distanceBetweenMax;
//public GameObject[] thePlatforms;
private int platformSelector;
private float[] platformWidths;
public ObjectPooler[] theObjectPools;
//public GameObject[] thePlatforms;
// Start is called before the first frame update
void Start()
{
//platformWidth = thePlatform.GetComponent<BoxCollider2D>().size.x;
//to search which of the platform has BC2D with sz
platformWidths = new float[theObjectPools.Length];
for(int i = 0; i < theObjectPools.Length; i++)
{
platformWidths[i] = theObjectPools[i].pooledObject.GetComponent<BoxCollider2D>().size.x;
}
}
// Update is called once per frame
void Update()
{
if (transform.position.x < generationPoint.position.x)
{
distanceBetween = Random.Range (distanceBetweenMin, distanceBetweenMax);
platformSelector = Random.Range(0, theObjectPools.Length);
transform.position = new Vector3 (transform.position.x + (platformWidths[platformSelector] / 2) + distanceBetween, transform.position.y, transform.position.z);
//Instantiate(/* thePlatform */ thePlatforms[platformSelector], transform.position, transform.rotation);
GameObject newPlatform = theObjectPools[platformSelector].GetPooledObject();
newPlatform.transform.position = transform.position;
newPlatform.transform.rotation = transform.rotation;
newPlatform.SetActive(true);
transform.position = new Vector3(transform.position.x + (platformWidths[platformSelector] / 2), transform.position.y, transform.position.z);
}
}
}
对象池
using System.Collections;
使用 System.Collections.Generic; 使用 UnityEngine;
公共类 ObjectPooler : MonoBehaviour { public GameObject pooledObject;
public int pooledAmount;
List<GameObject> pooledObjects;
// Start is called before the first frame update
void Start()
{
pooledObjects = new List<GameObject>();
for(int i = 0; i < pooledAmount; i++)
{
//casting mtd
GameObject obj = (GameObject) Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects.Add(obj);
}
}
public GameObject GetPooledObject()
{
for (int i = 0; i < pooledObjects.Count; i++)
{
if (!pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects.Add(obj);
return obj;
}
}
【问题讨论】:
-
Please do not post images of code because they are hard to use. 代码应作为文本直接发布在您的问题中。
-
我已经更新了它们。你能帮帮我吗谢谢你xx
-
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.到底有什么不明白的地方? -
由于在您的两个代码 sn-ps 中都没有破坏对象,因此该错误似乎是由您在某些对象上调用
Destroy的另一个错误引起的 -
所以我在 youtube 上遵循了一些基本教程。但在他的屏幕上,它似乎是平台继续制作它的克隆,但我的会在某个过程停止。 istg,我对编码没有任何问题。我会把链接分享给你youtube.com/…