Learn to code by making a 2D space shooter game.

Suleiman Abdullah
3 min readFeb 3, 2022

Objective: Implement Ammo collectible.

Before doing anything go to the window < package manager < 2D Sprite, install this.

The first step selects your sprite from the project < Assets, change from texture type to 2D and UI and sprite mode to singular then apply.

In the next step grab the sprite to the hierarchy which you want to be an ammo collectible and resize it.

The third step adds rigid body2D and box collider2D, the set is triggered to true and gravity scale to zero.

In the next step add powerup script add powerupID equal to three.

In the next step drag Ammo collectible to project < Assets < Prefab, to make this a prefabs.

The next step is to select ammo collectible to delete it, select Spawn_Manager game object change powerup prefab game object array number to 4 then drag collectible prefab to the powerup prefab in the index of three.

Create a Public method called AmmoCollectible with parameters, then assign current lasers equal to the parameter.

In order to update UI, we need to call UpdateAmmo inside the Ammo collectible after we assign to current laser to amount.

In the next step go to the powerup script when the player collides with powerup add another case then call that method Ammo Collectible then pass a parameter of no 15.

In the last step go spanwManager script change the random prefab no so to make sure power is included.

Note: Random. Range excludes the last number eg Random.Range(0,3) three are excluded to include you need to put four from the last number.

If you play the game everything will be working but we will get this error to avoid this error when playing the game when ammo is collected.

ArgumentNulleception

This is because the script is playing audio when we collide with the player and its senses the value is null.

Inside powerup

Note: I forgot to add the audio source and drag the clip there, to fix this drag the audio source to ammo collectible prefab then add a sound from the audio source < audio clip.

Let us see the final result.

see you in the next article

--

--

Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development