Deactivating Bodies Issue

Feb 16, 2011 at 4:48 AM

So i'm trying to do something very simple. I have a player, who shoots a gun, when the bullet hits the enemy the bullet is destroyed and the enemy loses health. I have my bullets in a list, and have their corresponding physics bodies in a list. I've programmed it so that when the bullet hits a target, it becomes inactive, but is recalled when fired again.

I assign the bullets a collision event with a body.OnCollision += OnCollision. And within my defined OnCollision I detect what the 2 fixtures are and what actions to take. When the bullet strikes anything in the game, which so far is just an enemy and a platform, it runs

fix1.Body.Enabled = false; 

Now this works perfectly when the bullet is colliding with the static ground. I look in debug and the bullet becomes un-enabled and greyed out and everything is fine when its re-enabled and called again. But when it collides with my dynamic enemy, I get an error "Object reference was not set to an instance of an object" within the Contact.cs file. I look and it turns out the values FixtureA, and FixtureB are null. 

Why is this happening? and why does it work when its colliding with my platform object?

Feb 16, 2011 at 3:51 PM

Not easy to say why this happens in your game, it could be a lot of things. The first thing that comes to mind is, are you returning false in the OnCollision method when you disable the body? if not, you should.

Feb 16, 2011 at 6:09 PM

I checked and nope I was always returning true, but I've fixed that now and the issue is still there.


Is there a better way of doing this sort of thing? would it make sense to just change the colliideswith to none instead and then re-assign it on creation?