The object of type 'GameObject' has been destroyed but you are still trying to access it. (906817)

Hi guys!

I’m new to unity and csharp, i have the following code which generates enemies and destroy them, however i get this error:

The object of type 'GameObject' has been destroyed but you are still trying to access it.

Here is my enemy generator code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemyGenerator : MonoBehaviour
{
    public GameObject original_enemy;
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        DecideIfEnemy();
       
    }
    private void DecideIfEnemy()
    {
        float random = Random.Range(0.0f, 100.0f);
        if (random < 1.0f)
        {
            GameObject.Instantiate(original_enemy, transform.position, transform.rotation);
        }
    }
}

and here is my enemy code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemy1 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(new Vector3(-0.005f, 0.0f));
    }
}

I don’t know what to do, i already tried to solve by myself but i can’t find or see the issue.. any idea?

Thank you! <3

This is just a variation on a bog-standard boring old nullref. The same three steps apply.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that