60 days to become a game developer. Day 25

Ryan Brechler
3 min readDec 7, 2020

--

Spawning Method with the Enemy!

Today I was back at it to understanding the Spawn Routine. We first discussed understanding the logic on how to get the code to implement the routine. So diving into Unity C Sharpe to get code we where looking for. IEnumerator is what we found. So we then started to type out the logic like below.

//spawn game object every 5 seconds

//Create a coroutine of type IEnumerator — Yield Events

//while loop

IEnumerator SpawnRoutine()

{

yield return null; //wait 1 frame

//then this line is called

yield return new WaitForSeconds(5.0f);

//then this line is called

//while loop (infinite loop)

//Instantiate enemy prefab

//yield wait for 5 seconds

}

We then went into explaining an infinite loop. While these are very dangerous to implement because it can error out and crash your game. It is needed if it is safely implemented. Below is the code to create a safe infinite loop.

IEnumerator SpawnRoutine()

{

while (true)

{

Vector3 posToSpawn = new Vector3(Random.Range(-8f, 8f), 7, 0);

Instantiate(_enemyPrefab, posToSpawn, Quaternion.identity);

yield return new WaitForSeconds(5.0f);

}

Remember we need to add the Serialized field so we can call the object. Do not forget to add the Enemy Prefab into the script on the spawn manager. Remember to create the field like below to make this happen.

[SerializeField]

private GameObject _enemyPrefab;

We then needed to create a new enemy object to clean up the spawning enemies. Because if we just let the enemies spawn they would be taking over the screen and constantly deploying new enemies. So in the SpawnManager in Unity I created a empty game object and named it Enemy Container.

[SerializeField]

private GameObject _enemyContainer;

Reminder this is all being done in the Spawn Enemy Script. Hoped back into unity and let unity compile and drop the enemy container into the spawn manager script like we did before with the enemy prefab. Implement the code below to start the routine to populate in the enemy container so it looks cleaner in Unity when spawning. Reference below.

Look at the spawn manager and the Enemy Container.

GameObject newEnemy = Instantiate(_enemyPrefab, posToSpawn, Quaternion.identity);

newEnemy.transform.parent = _enemyContainer.transform;

Final step was to create a method to when the player dies we need to stop the spawning of the enemies. We need to jump into the player script and communicate with the lives portion of the enemy script. Below is the notes to add to the lives in the player.

public void Damage()

{

_lives — ;

if (_lives < 1)

{

//Communicate with spawn manager

//let them know to stop spawning

Destroy(this.gameObject);

}

}

Jumped back in to create a Private bool and added into the spawn routine.

private bool _stopSpawning = false;

We created a new public void to put in the following code to start communicating with the player.

public void OnPlayerDeath()

{

_stopSpawning = true;

}

We went into creating the method in the player to stop the spawning. I am not going to lie. I did not understand the logic on this. But he explains that it will take time to understand and to practice creating this method and understanding it. Below is the video of the code we created and how it looks on the spawning of the enemy after the player dies.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Ryan Brechler
Ryan Brechler

Written by Ryan Brechler

I live my life by a quote I read off a wall in Iraq… “I refuse to tip toe through life only to arrive safely to death” Currently on the path to a new me!

No responses yet

Write a response