Object destorys itself when game is run. UGH.,

Hi all,

I’m doing a tutorial space shooter but have hit a snag and cannot after 3 hours of searching and recoding and searching recoding and… ARGH!

So here’s the deal. Basic blocks falling down and a block as the player (space invaders set up). I want the falling blocks to destroy themselves if they hit the player and I want the laser that the player fires to destroy the block. So in the tutorial I have ensured I have all of the rigidbody, tags and colliders set up exactly like they do. Then I put this code in, which is exactly what they do (and their’s works). Please… for the love of God tell me where I am going wrong.

When i start my game the falling block dissapears (destroys itself before colliding). This script is running in the enemyscript.

private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Destroy(this.gameObject);
}

if (other.tag == "Laser")
{
Destroy(other.gameObject);
Destroy(this.gameObject);
}

}

One thing, please don’t give me other or better ways to do this. For the moment I just want to get why THIS code is not working. Why it is jumping straight to destroy itself without hitting the OnTriggerEnter part.

Huge thanks in advance.

It sounds like they are hitting a collider. Have you tried adding some logging in your if statement to print out the name of the object that they are colliding with?

Thanks Sacredgeometry - sorry I should have posted I solved it. 4 hours it too lol. I love finding a bug, but man programming is head smashing on desk stuff.

It was a simple line of code that i forgot to // out after the tutorial taught me how to warp an object. So when the program starts it moved the object to 0,0,0… of course my enemy was spawning in at… drumroll… 0,0,0. So it was colliding at the beginning. Ugh… lol. 4 hours of going through my code over and over and over and FINALLY I noticed it.

I think I’m not a programmer.