Implementing enemy wave system — Part 2

C#
Unity
Unity
[SerializeField]
WaveSpawner[] waveSpawner;
int startingIndex = 0;
C#
  • The micro time passed between these two checks makes a world of difference because if the second check was not in place, even if the player would die, the message of the next level will be displayed regardless, which we do not want to :)
  • Second, we are yielding a coroutine in the end instead of the usual waitforseconds( ) because both acts are interconnected.
  • If you think a little deeper, the level check coroutine can not function until the previous level gets completed and the next level will not begin until the level check passes the number of levels. Both depend on each other to make things work. The only thing connecting them in between is the player’s death.
C#
C#
  • We have provided it with a data asset and in the first loop, we are getting the total number of enemies written in the asset via its’ method to retrieve the enemy count.
  • This means that the loop will not exit until the entire count is delivered in the game.
  • As soon as we get in, we are again checking if the player is still alive by the if statement.
  • Here, we instantiate the item that contains the type of enemy.
  • Since there are two different enemy prefabs in my project, there is a check for the name of the item and then it passes the written speed from the data set to the enemy script component by retrieving via a public method.
  • The associated enemy script takes care of the translation of enemy prefab in the game and has a speed variable that gets set via this step.
public void setEnemySpeed(float value)
{
_speed = value;
}
  • After all enemy items of each enemy type are delivered, the loop ends and it waits for 5 seconds before going back to the first coroutine.
  • The break keyword makes sure that the loop does not get into an infinite state after the full count of enemies of the first data asset/level/wave is over.
  • However, before returning to the first coroutine, the index counter gets an increment of 1 that basically says this coroutine is expecting the next data asset/wave/level.
Unity
   [SerializeField]
private Text _WaveText;
[SerializeField]
private UI_Manager uI_Manager;
Unity
C#
  • I am also setting the text property with a concatenation operator.
  • This coroutine then calls another coroutine that waits for 2 seconds and then disables the text component.
C#
C#

--

--

--

Work in Progress.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Cloned hard drive won t boot windows 10

Any way to free hand crop an image?

Data Center Migration : Strategy, Process and Solutions

Programming in Your Browser Is (Almost) Here

[Backend] Create a free machine on GCP

Developing an E-learning platform is easy with these ready-made clone scripts

Plan your goals SMART

Palindrome String

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
Samarth Dhroov

Samarth Dhroov

Work in Progress.

More from Medium

Quick and Easy: Alter Your Pre-Built Unity Texture Maps using Krita.

Talking to your unmanaged code efficiently from Unity using P/Invoke

Saving Data in Unity3D Using Files

Easy character animations in Unity3D