# Chapter 5: 3 Logic Errors in alien spawn function

**URL:** <https://forums.kodeco.com/t/chapter-5-3-logic-errors-in-alien-spawn-function/70685>\
**Category:** Unity Games by Tutorials\
**Created:** [May 13, 2019, 7:42am UTC](https://forums.kodeco.com/t/chapter-5-3-logic-errors-in-alien-spawn-function/70685 "2019-05-13T07:42:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![duckmole](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/duckmole/32/109222_2.png) [@duckmole](https://forums.kodeco.com/u/duckmole)\
**Post date:** [May 13, 2019, 7:42am UTC](https://forums.kodeco.com/t/chapter-5-3-logic-errors-in-alien-spawn-function/70685/1 "2019-05-13T07:42:06Z")

</div>

Hi, I found that the alien spawn function has 3 logic errors. (maybe I wrong)

1. 

```
 if (aliensPerSpawn > spawnPoints.Length)
             {
                 aliensPerSpawn = spawnPoints.Length - 1;
             }

```

「This limits the number of aliens you can spawn by the number of spawn points.」

My Opinion: This limits the number of aliens less 1 than number of spawn points, I think it should be

`aliensPerSpawn = spawnPoints.Length;`

1. `aliensPerSpawn = (aliensPerSpawn > totalAliens) ? aliensPerSpawn - totalAliens : aliensPerSpawn;`

「This means a spawning event will never create more aliens than the maximum amount that you’ve configured.」

My Opinion: If totalAliens is 1, aliensPerSpawn set 11, it will end be 10, still more than totalAliens. Should we first check the smaller number between totalAliens and spawnPoints, then limit aliensPerSpawn less than the smaller?

```
int smallerNumber = (spawnPoints.Length > totalAliens) ? totalAliens : spawnPoints.Length;
aliensPerSpawn = (smallerNumber > aliensPerSpawn) ? aliensPerSpawn : smallerNumber;

```

3.`int randomNumber = Random.Range(0, spawnPoints.Length - 1);`

「This line produces a random number as a possible spawn point.」

My Opinion: The Range doesn’t include spawnPoints.Length - 1, so the range will be 0-9, actually we have 0-10. So if we set aliensPerSpawn 11, the program will probably enter an infinite loop.We should modify as follow:

`int randomNumber = Random.Range(0, spawnPoints.Length);`

1. I don’t understand the usefulness of maxAliensOnScreen, what the difference with totalAliens? Hope you could explain, really appreciate.

@crispy8888 @vegetarianzombie

---

<div class="post-metadata">

**Author:** ![duckmole](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/duckmole/32/109222_2.png) [@duckmole](https://forums.kodeco.com/u/duckmole)\
**Post date:** [May 13, 2019, 7:56am UTC](https://forums.kodeco.com/t/chapter-5-3-logic-errors-in-alien-spawn-function/70685/2 "2019-05-13T07:56:01Z")

</div>

About point 4, I just search the aliensOnScreen in the book, and find in Chapter 8,

```
public void AlienDestroyed() 
{
  aliensOnScreen -= 1;
  totalAliens -= 1;
}

```

then the setting of maxAliensOnScreen does make sense.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 9, 2019, 1:45am UTC](https://forums.kodeco.com/t/chapter-5-3-logic-errors-in-alien-spawn-function/70685/3 "2019-07-09T01:45:31Z")

</div>

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [October 26, 2019, 11:42pm UTC](https://forums.kodeco.com/t/chapter-5-3-logic-errors-in-alien-spawn-function/70685/4 "2019-10-26T23:42:06Z")

</div>

Closed on October 26, 2019, 11:42pm UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
