-
-
Notifications
You must be signed in to change notification settings - Fork 31
Home
- Where can I download the sprites?
- Why not use colliders to detect when invaders hit the edge of the screen?
- Why can I not drag my prefabs into the array in the editor?
The sprites are included the full project / source code which can be downloaded with this link: https://github.com/zigurous/unity-space-invaders-tutorial/archive/refs/heads/main.zip
Download and unzip the project, then look for the sprites in the Assets/Sprites
folder.
We could use colliders to detect when invaders hit the edge of the screen, but this solution would be more complex for two main reasons.
-
The edge of the screen is going to be different depending on the player's screen resolution. This means we would need to write additional code to change the position of the colliders depending on the screen size. This code isn't too complicated, but it's still a bunch of excess code we can avoid.
-
To check for collision with the colliders, we would use
OnTriggerEnter2D
. We could handle this as part of theInvaders.cs
script or in theInvader.cs
script. Either way, it'll require more complex code. For the former, you need to make sure the invaders' collider is always positioned and sized correctly to match the grid anytime an invader is killed. For the later, you would need to implement a design pattern to communicate from an individual invader to the group as a whole.
You might think using colliders prevents needing much code at all, and in most cases that's true, but for this particular use case it ends up require more code and code that is more complex. Although we took a manual approach of checking when invaders hit the edge of the screen using custom code, it ends up being a lot simpler.
Here is the relevant code for the solution used in the video: https://github.com/zigurous/unity-space-invaders-tutorial/blob/main/Assets/Scripts/Invaders.cs#L89-L114
At around 25:20 in the video, I drag my prefabs into the array on the Invaders component. If you are unable to do this, it is most likely because the Invader.cs
script is missing from your prefabs. When we declare that array of prefabs in code, we specifically declare it as an array of Invader
, as seen here:
public Invader[] prefabs = new Invader[5];
This means the code is expecting each of the game objects to have the Invader.cs
script attached to it. The Unity editor is smart enough to know this, so it prevents you from assigning any game object to the array that is missing the script.