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

I am instantiating the prefabs time to time and I want them to be destroyed with in 2 to seconds as well. But I am getting the error that says:

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


So, somewhere script to trying to access the prefab even after it has been destroyed so probably I need to relocate the the statement where I am destroying the prefab using destroy .

Here is my code:

void Update()
{
if (!spawning)
{
timer += Time.deltaTime;
}
time = Random.Range (20, 40);
if (timer >= time)
{
StartCoroutine(Spawn());

}
}
public IEnumerator Spawn()
{
spawning = true;
timer = 0;
initial = new Vector3 (Random.Range(rmin, rmax), -0.23f, Random.Range(r.renderer.bounds.min.z, (r.renderer.bounds.max.z + 20)));
fuelprefab = (GameObject)Instantiate (fuelprefab, initial, Quaternion.identity);
yield return new WaitForSeconds (Random.Range (1, 3));
spawning = false;
Destroy (fuelprefab, Random.Range(1,3));
}
}


What change do I need to make so that it's not accessed after it hs been destroyed and the newly instantiated prefab is accessed?

The problem is that fuelprefab is the object passed to the Instantiate() command:
fuelprefab = (GameObject)Instantiate(fuelprefab etc.

Thus the code attempts to instantiate that object after destroying it. So what you want to do is have separate variables for fuelprefab and fuelInstance. Then you can freely Destroy() the latter while never destroying the former:
fuelInstance = (GameObject)Instantiate(fuelprefab, initial, Quaternion.identity);