Pokemon Unity Tutorial – More Pokemon [Pt.6]

August 19, 2019

Unity Pokemon Tutorial

Encounter List Creation

Youtube – Lets Make… Pokemon in Unity! – Episode 6 More Pokemon!

By: BrainStorm Games

This tutorial is about actually creating BasePokemon GameObjects and using them as a prefabs. The general prefab structure is pretty simple. The object is a SpriteRenderer (with the pokemon’s sprite) and has a BasePokemon script attached that just has all the base stats filled in in the editor, along with typing in the name and also hading a sprite reference here.

We created 5 different types of this prefab for different pokemon objects and used those to populate the GameManager BasePokemon list. It turns out this list won’t be holding every pokemon that exists in the game, this will just the list of possible encounters in the current area.

The method for encountering pokemon included creating an EmptyPoke prefab as well, which acts as a placeholder that becomes one of the referenced Pokemon objects from the current GameManager list. The battle instantiates this EmptyPoke object, and then uses the reference from the randomly selected Pokemon object from the list to basically add a BasePokemon component to this EmptyPoke object and copy all the relevant data over to it.

Finally, they added some transform objects to locate your pokemon and the opposing battle pokemon in the battle scene. These objects are just referenced as where to instantiate Pokemon objects (and as a result, their sprites).

Testing and Debugging

Problem 1: Generating Empty Lists that Were Being Accessed

I was getting an error that GetRandomPokemonFromList from attempting to access an array element out of the bounds of the current list being given to it. It turns out this is because I only put VeryCommon and Common rarity pokemon as options to be found, so when any other rarity (Rare, Semirare, VeryRare) were being rolled for the encounter, it was defaulting to trying to access the first element of an empty list.

Solution

I was able to solve this by adding a check for an empty list and having the method return null in that case, then the rest of the methods using this data would check if it was null first before performing their actions, so they would only proceed if they had an object to work with first.

Problem 2: Missing Encounters

I added my own EndBattle method to the GameManager script just to make testing multiple encounters easier and found an issue where it did not seem like every pokemon in the list was coming up as an encounter. I know RNGs can be a bit repetitive, so I tested for a while but it was clearly missing some encounters.

Solution

It turns out the Random.Range int they were using with bounds of 0 and list.count – 1 did not need the (- 1) since the high bound is an exclusive bound. With the (- 1), one pokemon from each rarity tier was being left out as an option. Removing the (- 1) fixed my issue, and all pokemon were appearing (eventually).