Game Object being destroyed, why?

Hello All,

I am really hoping someone can help me. My game is running to slow on 1st and 2nd generation iOS devices with CPU pegged out at 100%. So I started to do some optimizations, one being pooling all of the bullets being instantiated and destroyed. I started with my turret bullets and for some reason when they collide with something they are being destroyed. I have “find in files” of my entire project and commented out all of the Destroy(… statements, all of them, and the bullet is still being destroyed. Does anybody have any ideas why? Is there a setting I do not know about in the Unity editor? Thank you in advance.2038275--132106--ConsoleScreenshot.png

You are starting an Invoke. If the bullet gets destroyed before 5 seconds are passed, the bulletHide method is still executed, which lead to a null reference exception.
Alternatively you can start a coroutine which waits 5 seconds. As far as I know, coroutines are automatically terminated when the game object is destroyed. If not, you can just stop the coroutine.

Thank you for your quick reply. The problem is, I do not want the bullet to be destroyed. I just want to hide it after 5 seconds, which I commented out the code for now so I can move the bullet slowly trying to figure this out. As soon has it touches the ship the bullet is destroyed, but there is not script that should destroy it.

You are definitely destroying the bullet somewhere. You may search in your code for “Destroy”.

I have commented out every Destroy(…); statement in my whole entire project. Still being destroyed… Playing with the numbers more, it even destroys itself without a collision. Any other ideas???

An update…I think I found the line of code that is causing the destruction of my bullet. I am assigning a Vector3 to the Rigidbody.velocity. If this code is commented out, the bullets are not destroyed. When I uncomment this line, the bullets are destroyed. Why would assigning a velocity to the Rigidbody cause the game object to be destroyed?

1 Like

Another interesting update. I changed the code to use transform.position instead of rigid body.velocity and my bullets are no longer being destroyed. I really believe that there is a bug in the rigid body velocity property causing the destruction of my game objects. Anyone have thoughts on this?

1 Like

Because it might be destroyed on collision. So there is a Destroy or DestroyImmediate in your code somewhere that is responsible for that

DestroyImmediate too?

Unfortunately have never used DestroyImmediate in an script in my project.

Do you use any 3rd party extensions which contain scripts? There has to be something that destroys the bullet, because this never happens automatically.

No…I did not use any 3rd party code. I wrote it all myself. I just did a different test. Without changing any code at all, I created another prefab. Just a plain old capsule, my bullet is just a shere with a material. I attached the same rigid body and scripts to the new capsule prefab and set the tag and layer to be the same as my bullet prefab. The capsule prefabs are never destroyed. The game objects look identical with the same components and same scripts. I am thinking that my bullet prefab has some type of corruption. Is this possible?2039500--132262--CapsuleLog.png2039500--132262--CapsuleLog.png2039500--132263--BulletLog.png

I just upgraded my project to Unity 5…maybe the upgrade process did something?

It is very unlikely that game objects are automatically destroyed after the upgrade to Unity 5. Did you check ALL your scripts whether they contain Destory(Immediate)?

Yes…

Can you share your project?

I prefer not to share my whole project. I have a work around solution by deleting the prefabs that are being destroyed and recreating them. I have done this with the turret bullet and it is working like it should now. A little extra work, oh well. It is frustrating not knowing why though.

It is not possible to help without further information, the actual code or the actual project…

I have recreated all of my prefabs and the problem is gone. Thanks everyone for your assistance.

Just so you don’t feel like you were going crazy. I’m having this exact same issue. No destroy statements anywhere in the project. No third party plug ins. Was trying to implement object pooling and for some reason on collision my projectiles are destroyed.

No idea what I’ve missed.

1 Like