Player ship does not get destroyed

When I have developed this section of code it was working well - player ship got killed after a few hits.
But after I moved to the next lesson (Controlling collisions with layers) it just stopped working. The laser beams just move through the ship and terminate on the Shredder (so I guess projectile is working well and the ship is the cause of problem).

I have rechecked all inspector settings and they are ok, also layer collisions are set ok ({enemy laser - friend} and {frinedlaser - enemy})

In debugging i found out that collision happens - OnTriggerEnter2D is executed (prints a console line), but something goes wrong in the next sentence - the IF clause is NOT executed because the [missile == null] (this prints in console)

Projectile missile = collider.gameObject.GetComponent();
//print(missile);

I have copied the line from the EnemyBehaviour.cs where it is working (just in case as it was the same before) and it still does not work. The Projectile.cs code is just the same for the enemy and it works there fine.

This is driving me crazy - everything is identical as for the enemy, but it does not work. Again: it stopped working after working on the layers lesson, so I guess that is where the bug is hiding, but i just have no clue why. Any help or idea is appreciated.

Hi Alfred,

I would check all of the layers for the specific objects, so, enemy, enemy projectile, player and player projectile, and also the transform of the enemy projectiles, specifically the Z index.

I would also be using;

    Projectile missile = collider.gameObject.GetComponent<Projectile>();

e.g. specifying the Type of the component you are trying to retrieve.

Also, check to see that there is a Projectile script on the enemy laser prefab that you are instantiating.

Also - do you have IsTrigger set on the Polygon Collider 2D component on the Player?

After doing this, if you are still having difficulties, zip the project up and share a link to it here and I will have a look for you. If the zip if less than 10mb you can upload it directly into your reply. If it’s greater than 10mb then you will need to use something like Google Drive / One Drive / Dropbox and provided a link.

Please ensure that the entire project is zipped. :slight_smile:

1 Like

Hi Rob
Thanks a bunch - i got it thanks to you. I made a separate script for enemy projectiles (when i was building my version before the solution) so i can give them different attributes and then I have copied the code with script for the player projectile.

So the solution is:

EnemyProjectile missile = collider.gameObject.GetComponent<EnemyProjectile>();

Thank again for the help

1 Like

Well done on resolving it and you’re more than welcome. Glad you can move forwards again :slight_smile:

1 Like

I had the same problem and resolved it as follows.

There is also a mistake in the video where the player script utilises a GameObject called projectile, i.e

public GameObject projectile;

But “Projectile” does not show in the training video Unity Inspector - the training video Inspector shows this as “Laser”. Somewhere along the way someway chopped the video and spliced in something different using either “Laser” in the Inspector, or “projectile” in the player script.

I noticed that “projectile” is also used in the enemy behaviour script. Perhaps this is clashing with the player script as player projectiles and enemy projectiles co-exist. I renamed this to laser in the player script, i.e.:

public GameObject laser;

(insuring all references to projectile were also changed in the script), and the problem was resolved

Privacy & Terms