Learn to code by making a 2D space shooter game.
Objective: Implement Negative Pickup.
Before doing anything drag the sprite change texture type to Sprite 2D and UI, add rigidbody2D set gravity scale to zero, and add boxcollider2D check is trigger, then last add Powerup script.
The first step drags the negativepowerup which is Mine to the prefab folder.
In the next step, go to the Player script and create a public method called NegativePowerup.
Now we need to call the take damage method inside the NegativePowerup method.
In the next step select, the negative powerup game object in the hierarchy then adjusts PowerupID to 5, then apply the changes to the prefab.
Then add an audio source to the negativepowerup cos if you forget to add an audio source to gameObject with a script that will play an audio source you will get an error.
To fix this we need to add an audio source component to the negativepowerup and put the required audio clip.
In the next step, select spawnManager adjust the size of the array of powerup to 5, then drag and drop the negative powerup from the prefab folder to an index number 4, because in programming counting starts from zero and forth then delete the negative powerup from the hierarchy.
Note: don't drag negative power up from the hierarchy to the powerup array to be instantiated in the spawn manager you will get weird behavior because instantiating is the process of duplicating the game object from a prefab and not from the hierarchy.
In the next step, go to the powerup scrip create case 5 then call the NegativePowerup method in there.
In the Last step go to the spawn manager-script, to spawn powerup since its use random.range method to spawn random powerup, we need to add 5 as max no to include mine as negative powerup since the max number is excluded in random.Range since in array starts to count 0 to forth, the max number needs to be 5 to include negative powerup.
Let's see the final result.
Thank you for reading up here, See you in the next article stay safe bye