# Not able to destroy the prefab

In the code, everything is working smoothly, only problem I am facing is that the Destroy(fuelprefabInstance) is not getting Destroyed in OnTriggerEnter Method though the print statements are clearly executed and Destroy in Spawn method works fine as well. What am I missing?

Edit: It infact gives NullReferenceException if I try to set fuelprefabInstance to false instead of destroying the prefab using fuelprefabintsnace.setActive(false);

  public IEnumerator Spawn()
{
time1 = Random.Range (1, 5);
spawning = true;
timer = 0;
initial = new Vector3 (Random.Range(rmin, rmax), 0.037f, Random.Range((-7.608864f + 10f),(r.renderer.bounds.max.z + 20)));
fuelprefabInstance = (GameObject)Instantiate (fuelprefab, initial, transform.rotation);
yield return new WaitForSeconds (time1);
spawning = false;
Destroy (fuelprefabInstance, time1);  //this destroy works
}

void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
print("called");
Destroy (fuelprefabInstance);     //this Doesnot
print ("destroyed");
}
}

• why downvote :/ – Simran kaur Sep 3 '14 at 22:51
• Simran, this sounds like more of a debugging question. Have you attached the debugger and checked the state of fuleprefabInstance? Are you familiar with how to use the debugger? – MichaelHouse Sep 3 '14 at 23:29
• @Byte56: No, I am not really familiar with using Debugger, would be glad to get some reference to that as well or anything that can help regarding the issue.As of now, It appears that fuelprefabinstance is null inside of OnTriggerEnter but nt able to figure out the reason – Simran kaur Sep 3 '14 at 23:34
• Likely because you're destroying it already at the end of the Spawn() method. – MichaelHouse Sep 3 '14 at 23:47
• @Byte56:Not really, because it would not be visible if it was destroyed before, It becomes invisible when it's destroyed and if the OnTriggerEvent is being triggered, I suppose that is not the possibility. – Simran kaur Sep 5 '14 at 1:26