【问题标题】:Ive seen many of this error solution but i couldnt find any that could help me solve this. Really hope any of you can help. Thank you [duplicate]我已经看到了很多这样的错误解决方案,但我找不到任何可以帮助我解决这个问题的方法。真的希望你们中的任何人都可以提供帮助。谢谢[重复]
【发布时间】: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/…

标签: c# unity3d 2d


【解决方案1】:

您应该写一些代码以便更好地理解。除非绝对必要,否则我通常不会使用 NEW 运算符创建对象实例。这使您免于销毁您在堆内存中创建的对象的任务。

【讨论】:

  • 我更新了代码。我把 ObjectPooler 代码放在那里。你能帮我吗?请xx
猜你喜欢
  • 2019-08-30
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-03
相关资源
最近更新 更多