Error 'GameObject' has been destroyed but you are still trying to access it

Trying to figure this out with no luck. When my “Shredder” destroys the gameObject (coll.gameObject) - which should be the single instance- I get that error and my cactus and gnome stop shooting.

I attached my scripts. The compiler says the error is under Shooter on line 12 that says
12 {"GameObject newProjectile = Instantiate (projectile); }

Thanks for any help.


void Fire()
		GameObject newProjectile = Instantiate (projectile) as GameObject;
		newProjectile.transform.parent = projectileParent.transform;
		newProjectile.transform.position = transform.Find ("Gun").position; 


Shredder :

void OnTriggerEnter2D(Collider2D coll)
	DestroyObject (coll.gameObject);
1 Like

Hello, the 2d collision matrix is allowing the collision between the projectile and the ship?

1 Like

So far everything is colliding with my projectiles okay. Here is my projectile code.

Thanks for looking into this : ) .

public class Projectiles : MonoBehaviour
public float speed=2;
public float damage= 5;

void OnTriggerEnter2D (Collider2D coll)
	print (name + "Collided with " + coll); 
void Update()
	transform.Translate ( Vector3.right * speed * Time.deltaTime); 



But is something preventing your creatures from colliding with the projectile?
Seems that something is destroying the projectile sooner than it should, the projectile is actually fired?


Oh my god, sorry, I thought it was about the laser defender class, just sec, will restart my brain and help you out :joy:

1 Like

You could try a few things, I see that you are using DestroyObject instead of just Destroy, and DestroyObject is an old and deprecated method, you could try changing it out to Destroy.

You could add a small timer to the Destroy method, it would be a quick fix if it actually work. The Destroy(object target, float timer) has an overload that allow you to add a timer to be counted before destroying something, it could fix a few problems that might happen in the actual physics loop step or update loop step after something Collider and get destroyed, try changing the DestroyObject(cool.gameObject); to Destroy(cool.gameObject, 0.2f);

Another thing that could be happening is that you could be deleting the wrong GameObject, check if the Collider is within the root of the projectile (the parent and not the child). If the Collider is within the child, then you are destroying just the child and the parent will continue to exist (which could cause some problems.

Sorry for not remembering a lot about those games structure :sweat_smile:

The console won’t always point to the right place for us to look (but in the majority of the times it will).

1 Like

Awesome. Thanks for your help! Figured out it was actually me having a projectile prefab exist in the hierarch. When I erased it- it worked. Not sure why but I’ll ponder that another time lol.

Thanks again- noticed because of your helpful questions getting my brain on track.

I’ll be posting another question soon - so stay tuned! lol

1 Like

Happy for being able to help one way or another :joy:

Sure, just ask it, I’ll try to help whenever I can :smiley:

Privacy & Terms