Skip to content
Adam Graham edited this page Jan 20, 2022 · 30 revisions

FAQs


Why do my asteroids not pass through the boundaries?

If your asteroids are unable to pass through the boundaries, then this most likely means the two are objects are colliding, but we actually want collision to be disabled between these objects. To do this, we set the game objects to be on different physics layers so we can turn off collision between the two layers in the physics settings.

The asteroids should have their layer set to "Asteroid" and the boundaries should be set to "Boundary". Make sure to set this on the prefabs if you have them so it applies to all instances of the game object. After doing this, we can go to Edit > Project Settings > Physics 2D and uncheck collision between Asteroid/Boundary in the collision matrix.


Why does nothing happen when bullets hit asteroids?

In the Asteroid.cs script, the code is checking when the asteroid collides with a bullet by comparing the tag of the incoming object. If nothing is happening when a bullet collides with an asteroid, it might be that you have forgotten to set the tag of your bullet prefab to "Bullet", or there could simply be a mistake in the name or spelling.

It is also possible that you are comparing the name of the incoming game object rather than the tag, gameObject.name vs gameObject.tag. It's generally a better practice to use tags over names.

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("Bullet"))
    {
        //...
    }
}
Clone this wiki locally