Randomizing Power Ups

In this article, I am going to cover how to randomize the power ups that are dropped when enemies are destroyed in my game. The desired behaviors will be…

  • When the enemy is destroyed a random number will be selected and depending on that number a certain power up will be spawned.

Let’s dive in!

In each of the enemy scripts, this code will be added since I changed the Power Ups being spawned from a destroyed enemy instead of spawning on their own. I wrote about this is in previous story linked below.


The first we are going to declare a few variables. The first is an array of integers that will hold the likely hold of a certain power up to be picked. Now, naming it percentage chance is misleading because the numbers don’t stand for an actual percentage chance. The second integer is going to be the total of each of the percentage chance integers in the array.

Since we gave the percentage chance a SerializedField attribute we can adjust the size of the array and the value of each of the elements in the Inspector. The size of the array needs to match the array size of the power ups which is 7. We will give each element a value and this will determine which power up is selected. This will make more sense in a bit.

Back in the enemy scripts we are adding more to the public SpawnPowerUp method. First, we are checking to see if the enemyDead is true, since some enemies can take multiple hits and I don’t want it spawning for each hit of the enemy. Using a foreach loop, we will loop through each of the numbers in percentage chance array, which is represented by the variable weight and we will add that weight to the total. So, adding up all the numbers for reach element in the array will be 70 + 60 + 50 + 40 + 30 + 20 + 10 = 280. The total is 280.

We are going to pick a random number and store it in the a local variable. The random range will be from 0 to the total of all the elements in the array (280).

Using a for loop, we are loop through each of the elements in the percentage chance array and are checking to see if the random number that was picked is < or = to the current value of the element in the array. So if 65 is the random number than element 0 is picked since the value is 70 and 65 is < 70. This goes down the list. If 65 is picked then…

We are going to Instantiate the power up that is in the same of the array as the percentage chance array. For example, element 0 in the percentage chance is = to element 0 in the power up array (Element 1 = Element 1, Element 2 = Element 2, etc.). The power up spawn position is covered in the previous mentioned above, but essentially it is the position of the enemy before it was destroyed.

If the random number is 80 then it is not less that element 0 value of 70, so the else handles it will take the random number and subtract that 70, ending up with 10. The for each loop will loop through each of the values in the array to see which is < or = 10. This will result in element 6. These percentage chance values can be changed for each enemy, so you can increase the likelihood of a more powerful power being spawned when a more powerful enemy is destroyed. Hopefully that helps anyone looking to create a randomizing element to their game.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store