0

I'm trying to destroy cloned prefabs when they reach the bound on the x axis of a cube. But when the first clone is destroyed, an error occur, and the next appearing clone won't destroy.

Error:

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. deploy.Update () (at Assets/deploy.cs:52)

This is the script attached to the cube:

public class deploy : MonoBehaviour
{
    public GameObject starsPrefab;    
    private GameObject star;
    public float respawnTime=40.0f;

    Vector3 bsize,bmax,bmin,plane;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(starsWave());
        Collider bCollider = this.GetComponent<Collider>();

        bsize=bCollider.bounds.size;
        bmax=bCollider.bounds.max;
        bmin=bCollider.bounds.min;

        //Vector3 plane=new Vector3(bmax.x-bmin.x,bmax.y-bmin.y,bmax.z-bmin.z);
    }

    private void spawnStar()
    {
        star =Instantiate(starsPrefab) as GameObject;
        star.transform.position= new Vector2(bsize.x,Random.Range(-bsize.y,bsize.y));
    }
   
    IEnumerator starsWave()
    {
        while (true)
        {
            spawnStar();

            yield return new WaitForSeconds(respawnTime);
        }
    }

    void Update()
    {
        if(star.transform.position.x<bmin.x)
        {
            Destroy(star);
        }
    }

If I increase respawnTime, the same error occurs; the first clone appears, it reaches the bound, it's destroyed, there's the same error, a second clone appear afterwards, it reaches the bound, it's destroyed, there's the same error,&hellip

It seems that Destroy() is applied to the last cloned star, how can I apply this to any cloned star?

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • You spawn potentially many stars but only remember the last spwned. – BugFinder Dec 25 '20 at 19:09
  • Is there any way to remember all stars? – lemmecatch Dec 25 '20 at 19:17
  • 1
    @lemmecatch use a `List` instead a single reference? – derHugo Dec 25 '20 at 20:40
  • @derHugo thank you for your suggestion I'm going to try, I 'm new to coding and unity so I don't know exactly where to look for – lemmecatch Dec 26 '20 at 13:59
  • Words like "solved" and "updated" are not to be put in Stack Overflow question titles. The revision history indicates when a post has been "updated", and if the question has been answered, the answer posted below the question that best addresses the question should be accepted (click check-mark button). If you have solved the question yourself, post your own answer and accept it. Don't post solutions in the question; it makes them hard for others to find and read. – Peter Duniho Jun 30 '21 at 03:21

1 Answers1

-1

You are destroying star but you are still checking his position in the Update()

You can easily fix this by changing your if with

 `if(star!= null)`
{
if(star.transform.position.x<bmin.x)
{
Destroy(star)
}}

So if star is destroyed, it is no longer checked

Leoverload
  • 1,194
  • 3
  • 8
  • 21
  • Hello thank you for your quick reply, I just changed that but the problem is the same – lemmecatch Dec 25 '20 at 18:42
  • uh it's because you are first checking the star, look at my answer edited :D @lemmecatch – Leoverload Dec 25 '20 at 18:44
  • There's no error. I decreased the speed of star moving in negative direction on x axis ( a script is attached to the starPrefabs with a float speed), but none of the star will destroy they continue to move outside the bmin.x – lemmecatch Dec 25 '20 at 18:59
  • with the new code it will work, check my edit @lemmecatch – Leoverload Dec 25 '20 at 19:00
  • Yes I tried with the new code, but it works only one at a time so if there is more than 1 star visible, destroy doesn't work (I played with the speed here) If there s only one passing then the code works (it goes quickly and it destroys before a second star appear) – lemmecatch Dec 25 '20 at 19:12
  • You should **never** use a `!=null` check for anything deriving from `UnityEngine.Object` see e.g. [here](https://stackoverflow.com/a/60189474/7111561) or [here](https://stackoverflow.com/questions/56875706/how-am-i-misusing-the-null-coalescing-operator-is-this-evaluating-null-correc) – derHugo Dec 25 '20 at 21:22