Game Dev in Progress… Day 4

Cristian Aspacio
3 min readOct 27, 2020

--

Photo by Toa Heftiba on Unsplash

Hello, its Monday and another start to hopefully a wonderful week. Today I have had the chance to go through the first class course that GameDevHQ has created for us. The first course is an introduction in developing games using Unity with C#. We do this by going through the steps into creating our first 2D Space Shooter game. I have managed to go through about 30–40% of this course and I will go through some of the problems that I faced, how I solved and what I learned from it.

The first hard problem that I came across in this course is how to use the IEnumarator method provide by the Unity Engine. These method is a part of the MonoBehaviour class that all Unity script derive from. This means this method is available to all your C# Scripts when you’re developing for Unity. The two methods that are 99% always going to be in your scripts are Start() and Update(). Start is called whenever an GameObject with that Script is created or instantiated before the first frame. The Update() gets called every frame the GameObject exists in the world/scene. Now IEnumarator are also method and I will explain what it does.

IEnumarator is a Coroutine and a Coroutine is a function that has the ability to pause execution and return control to Unity but continue where it left off on the next frame. So what does this mean? At first I didn’t really understand but after looking over the Scripting API and some other resources explaining it online, I’ve come to understand it a bit more. Here is my definition to what a Coroutine is: A Coroutine is pretty much just a function that allows you to pause, wait for a condition or action, and then continue execution. This is very handy because most of your code is going to be run in the Update function. However, all code in the Update function is executed in a single frame. So if you ever wanted to delay an action or pause and resume in another frame, this would be very difficult to do using just the Update function or even a regular one. How do we use Coroutines? Well that’s where IEnumarator comes in.

The method IEnumarator is how Unity is able to run a function on multiple frames. When you use IEnumarator you need to implement a while loop inside the function and also determine where you will pause the execution of the process. To do this, you use the keyword yield and return. These two keywords will indicate that on this line on the inside of the while loop, I will pause execution. The following code after these two keywords will determine how long to pause. In the example we did for the course, I used WaitForSeconds(float seconds). These instructs Unity to pause and wait for the duration of the seconds given. For example, if we pass in 5 inside the function, Unity will know to wait for 5 seconds before continuing again. Here’s my code that I wrote to spawn an enemy every 5 seconds:

private IEnumerator SpawnRoutine(){while(_stopSpawning == false){float randomX = Random.Range(-9.0f, 9.0f);GameObject newEnemy = Instantiate(_enemyPrefab, new Vector3(randomX, 8, 0), Quaternion.identity);newEnemy.transform.parent = _enemyContainer.transform;yield return new WaitForSeconds(5.0f);}}

As you can see I used the yield return followed by how much I wanted to wait before spawning a new enemy.

Anyways, I hope this article was useful to understanding how Coroutines work and the IEnumarator function. Also, don’t forget to call the function by using StartCoroutine() and passing in the function in order for it to work. I called this method in the Start() function as it makes more sense to me then calling it every frame in Update(). I’ll see you guys all next time!!!

--

--

No responses yet