-1

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. ObjectPooler.GetPooledObject () (at Assets/Scripts/ObjectPooler.cs:31) PlatformGenerator.Update () (at 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);

        }
    }
}

ObjectPooler

using System.Collections;

using System.Collections.Generic; using UnityEngine;

public class 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;

}

}

Faiqxx
  • 1
  • 2
  • [Please do not post images of code because they are hard to use.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) Code should be posted directly **as text** in your question. – MikeCAT Jun 26 '21 at 19:51
  • Ive updated them. can you help me out please thank you xx – Faiqxx Jun 26 '21 at 20:14
  • What exactly do you not understand in `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.` ? – derHugo Jun 26 '21 at 20:24
  • Since in none of your two code snippets you are destroying objects the error seems to be caused by another one where you call `Destroy` on certain objects – derHugo Jun 26 '21 at 20:25
  • So i followed some basic tutorial on youtube. But in his screen it appears to be the platform keep on making its clone but mine will stop at certain process. istg, i have nothig wrong with the coded. I will share you the link https://www.youtube.com/watch?v=YznQRjbIpLU&list=PLiyfvmtjWC_XmdYfXm2i1AQ3lKrEPgc9-&index=9 – Faiqxx Jun 26 '21 at 20:26
  • Sounds to me like some script is destroying your pooled objects so the list elements still exist but are now `null` ... – derHugo Jun 26 '21 at 20:27
  • what do i do now? omg >. – Faiqxx Jun 26 '21 at 20:30
  • Help me to fix this issues pls : – Faiqxx Jun 26 '21 at 20:39

1 Answers1

0

You should put down some code for better understanding. I normally do not create an Object Instance using NEW operator unless it is absolutely necessary. This saves you from the task to destroy the object that you have created in heap memory.

rrajanuk
  • 27
  • 8