# I am stuck at level 9 in game dev 3 step 3: Enemies #level-help

**URL:** <https://discourse.codecombat.com/t/i-am-stuck-at-level-9-in-game-dev-3-step-3-enemies-level-help/35722>\
**Category:** Level Help\
**Created:** [June 8, 2023, 11:47am UTC](https://discourse.codecombat.com/t/i-am-stuck-at-level-9-in-game-dev-3-step-3-enemies-level-help/35722 "2023-06-08T11:47:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cong\_Vinh\_Tran](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cong_vinh_tran/32/34408_2.png) [@Cong\_Vinh\_Tran](https://discourse.codecombat.com/u/Cong_Vinh_Tran)\
**Post date:** [June 8, 2023, 11:47am UTC](https://discourse.codecombat.com/t/i-am-stuck-at-level-9-in-game-dev-3-step-3-enemies-level-help/35722/1 "2023-06-08T11:47:28Z")

</div>

Here is my code

```python
game.spawnXY("forest", -4, 64)
game.spawnXY("forest", 4, 64)
game.spawnXY("forest", 12, 64)
game.spawnXY("forest", 20, 64)
game.spawnXY("forest", 28, 64)
game.spawnXY("forest", 36, 64)
game.spawnXY("forest", 44, 64)
game.spawnXY("forest", 52, 64)
game.spawnXY("forest", 60, 64)
game.spawnXY("forest", 68, 64)
game.spawnXY("forest", 76, 64)
game.spawnXY("forest", -4, 4)
game.spawnXY("forest", 4, 4)
game.spawnXY("forest", 12, 4)
game.spawnXY("forest", 20, 4)
game.spawnXY("forest", 28, 4)
game.spawnXY("forest", 36, 4)
game.spawnXY("forest", 44, 4)
game.spawnXY("forest", 52, 4)
game.spawnXY("forest", 60, 4)
game.spawnXY("forest", 68, 4)
game.spawnXY("forest", 76, 4)

def onUpdateStatic(event):
    thing = event.target
    thing.pos.x -= 0.6
    if thing.pos.x < -4:
        if thing.type == "forest":
            thing.pos.x = 84
        else:
            thing.destroy()

game.setActionFor("forest", "update", onUpdateStatic)

def spawnRandomY(type):
    y = game.randomInteger(12, 56)
    spawn = game.spawnXY(type, 80, y)
    spawn.on("update", onUpdateStatic)

def spawnFences():
    spawnNumber = 1 + (game.time / 10)
    while spawnNumber >= 1:
        spawnRandomY("fence")
        spawnNumber -= 1

# GAME SETTINGS
game.spawnFenceTime = 0
game.spawnGemTime = 0.5
# One more spawn timer, for ogres.
game.spawnOgreTime = 1
game.score = 0
game.topScore = db.get("topScore") or 0
ui.track(game, "time")
ui.track(game, "score")
ui.track(game, "topScore")
goal = game.addManualGoal("Survive at least 20 seconds.")

# PLAYER
player = game.spawnPlayerXY("captain", 20, 34)
player.maxSpeed = 20

def onCollect(event):
    item = event.other
    if item.type == "gem":
        game.score += 5

def onCollide(event):
    unit = event.target
    other = event.other
    if other.type == "fence":
        unit.defeat()

def onDefeatPlayer():
    if game.time > 20:
        game.setGoalState(goal, True)
    else:
        game.setGoalState(goal, False)
    setTopScore()

player.on("collect", onCollect)
player.on("collide", onCollide)
player.on("defeat", onDefeatPlayer)

# ENEMIES
# This handles defeated ogres.
def onDefeatOgre(event):
    unit = event.target
    game.score += 10
    # Defeated ogres should move to the left, just like other environmental objects.
    unit.on("update", onUpdateStatic)

# This updates the positions of the ogres so they slowly catch up to the player.
def onUpdateOgre(event):
    unit = event.target
    # Only update position for un-defeated ogres, who have positive health.
    if unit.health > 0:
        # Set the unit closer to the player. We use the baseX property to track this, because the default ogre behavior might also change it's pos.x property, so we can't use that directly.
        unit.baseX += 0.1
        # If unit.baseX greater than 18:
        if unit.baseX > 18:
            # Set unit.baseX to 18:
            unit.baseX = 18
        # Set unit.pos.x to unit.baseX:
        unit.pos.x = unit.baseX

def spawnOgre():
    y = game.randomInteger(12, 56)
    unit = game.spawnXY("scout", 0, y)
    unit.baseX = 0
    # Assign onUpdateOgre to handle the "update" event on unit:
    onUpdateOgre("unit")
    # Assign onDefeatOgre to handle the "defeat" event on unit:
    onDefeatOgre("unit")
    unit.on("collide", onCollide)
    unit.behavior = "AttacksNearest"

# GAME LOOP
def checkPlayer():
    if game.time == 20:
        player.say("Win! Bonus time!")
    player.pos.x = 20

def setTopScore():
    topScore = db.get("topScore") or 0
    if game.score > game.topScore:
        db.set("topScore", game.score)

def checkSpawns():
    if game.time > game.spawnFenceTime:
        spawnFences()
        game.spawnFenceTime += 1
    if game.time > game.spawnGemTime:
        spawnRandomY("gem")
        game.spawnGemTime += 1
    if game.time > game.spawnOgreTime:
        spawnOgre()
        game.spawnOgreTime += 2

def onUpdateGame(event):
    checkPlayer()
    checkSpawns()
    game.score += event.deltaTime

game.on("update", onUpdateGame)

```

I tried but line 100 said TypeError: Cannot read propety health of unidentified. How can I fix this problem ?

 ![malfuntion.PNG](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/2/c/2c16de2d5af81a807d156d6fc3245d318e2f5b54.jpeg)

---

<div class="post-metadata">

**Author:** ![MarmiteOnToast](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/marmiteontoast/32/46649_2.png) [@MarmiteOnToast](https://discourse.codecombat.com/u/MarmiteOnToast)\
**Post date:** [June 8, 2023, 12:20pm UTC](https://discourse.codecombat.com/t/i-am-stuck-at-level-9-in-game-dev-3-step-3-enemies-level-help/35722/2 "2023-06-08T12:20:56Z")

</div>

> [@Cong\_Vinh\_Tran](#):
>
> ` onUpdateOgre("unit")`

> [@Cong\_Vinh\_Tran](#):
>
> ` onDefeatOgre("unit")`

I think that you just need to remove the speech marks (“”) around `unit`, since those make it a string and a string doesn’t have the property `health`

---

<div class="post-metadata">

**Author:** ![Cong\_Vinh\_Tran](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cong_vinh_tran/32/34408_2.png) [@Cong\_Vinh\_Tran](https://discourse.codecombat.com/u/Cong_Vinh_Tran)\
**Post date:** [June 8, 2023, 1:06pm UTC](https://discourse.codecombat.com/t/i-am-stuck-at-level-9-in-game-dev-3-step-3-enemies-level-help/35722/3 "2023-06-08T13:06:36Z")

</div>

Here is my new code:

```python
# Step 3 of the Run and Dodge game tutorial.

# ENVIRONMENT
game.spawnXY("forest", -4, 64)
game.spawnXY("forest", 4, 64)
game.spawnXY("forest", 12, 64)
game.spawnXY("forest", 20, 64)
game.spawnXY("forest", 28, 64)
game.spawnXY("forest", 36, 64)
game.spawnXY("forest", 44, 64)
game.spawnXY("forest", 52, 64)
game.spawnXY("forest", 60, 64)
game.spawnXY("forest", 68, 64)
game.spawnXY("forest", 76, 64)
game.spawnXY("forest", -4, 4)
game.spawnXY("forest", 4, 4)
game.spawnXY("forest", 12, 4)
game.spawnXY("forest", 20, 4)
game.spawnXY("forest", 28, 4)
game.spawnXY("forest", 36, 4)
game.spawnXY("forest", 44, 4)
game.spawnXY("forest", 52, 4)
game.spawnXY("forest", 60, 4)
game.spawnXY("forest", 68, 4)
game.spawnXY("forest", 76, 4)

def onUpdateStatic(event):
    thing = event.target
    thing.pos.x -= 0.6
    if thing.pos.x < -4:
        if thing.type == "forest":
            thing.pos.x = 84
        else:
            thing.destroy()

game.setActionFor("forest", "update", onUpdateStatic)

def spawnRandomY(type):
    y = game.randomInteger(12, 56)
    spawn = game.spawnXY(type, 80, y)
    spawn.on("update", onUpdateStatic)

def spawnFences():
    spawnNumber = 1 + (game.time / 10)
    while spawnNumber >= 1:
        spawnRandomY("fence")
        spawnNumber -= 1

# GAME SETTINGS
game.spawnFenceTime = 0
game.spawnGemTime = 0.5
# One more spawn timer, for ogres.
game.spawnOgreTime = 1
game.score = 0
game.topScore = db.get("topScore") or 0
ui.track(game, "time")
ui.track(game, "score")
ui.track(game, "topScore")
goal = game.addManualGoal("Survive at least 20 seconds.")

# PLAYER
player = game.spawnPlayerXY("captain", 20, 34)
player.maxSpeed = 20

def onCollect(event):
    item = event.other
    if item.type == "gem":
        game.score += 5

def onCollide(event):
    unit = event.target
    other = event.other
    if other.type == "fence":
        unit.defeat()

def onDefeatPlayer():
    if game.time > 20:
        game.setGoalState(goal, True)
    else:
        game.setGoalState(goal, False)
    setTopScore()

player.on("collect", onCollect)
player.on("collide", onCollide)
player.on("defeat", onDefeatPlayer)

# ENEMIES
# This handles defeated ogres.
def onDefeatOgre(event):
    unit = event.target
    game.score += 10
    # Defeated ogres should move to the left, just like other environmental objects.
    unit.on("update", onUpdateStatic)

# This updates the positions of the ogres so they slowly catch up to the player.
def onUpdateOgre(event):
    unit = event.target
    # Only update position for un-defeated ogres, who have positive health.
    if unit.health > 0:
        # Set the unit closer to the player. We use the baseX property to track this, because the default ogre behavior might also change it's pos.x property, so we can't use that directly.
        unit.baseX += 0.1
        # If unit.baseX greater than 18:
        if unit.baseX > 18:
            # Set unit.baseX to 18:
            unit.baseX = 18
        # Set unit.pos.x to unit.baseX:
        unit.pos.x = unit.baseX

def spawnOgre():
    y = game.randomInteger(12, 56)
    unit = game.spawnXY("scout", 0, y)
    unit.baseX = 0
    # Assign onUpdateOgre to handle the "update" event on unit:
    onUpdateOgre(unit)
    # Assign onDefeatOgre to handle the "defeat" event on unit:
    onDefeatOgre(unit)
    unit.on("collide", onCollide)
    unit.behavior = "AttacksNearest"

# GAME LOOP
def checkPlayer():
    if game.time == 20:
        player.say("Win! Bonus time!")
    player.pos.x = 20

def setTopScore():
    topScore = db.get("topScore") or 0
    if game.score > game.topScore:
        db.set("topScore", game.score)

def checkSpawns():
    if game.time > game.spawnFenceTime:
        spawnFences()
        game.spawnFenceTime += 1
    if game.time > game.spawnGemTime:
        spawnRandomY("gem")
        game.spawnGemTime += 1
    if game.time > game.spawnOgreTime:
        spawnOgre()
        game.spawnOgreTime += 2

def onUpdateGame(event):
    checkPlayer()
    checkSpawns()
    game.score += event.deltaTime

game.on("update", onUpdateGame)

```

I tried to remove the speech mark but it says cannot read propety health of null. How can I fix that ?

 ![sa.PNG](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/f/1/f1f2abeb32f0cada6d52c7467b72cf049a1bf846.jpeg)

---

<div class="post-metadata">

**Author:** ![MarmiteOnToast](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/marmiteontoast/32/46649_2.png) [@MarmiteOnToast](https://discourse.codecombat.com/u/MarmiteOnToast)\
**Post date:** [June 8, 2023, 1:12pm UTC](https://discourse.codecombat.com/t/i-am-stuck-at-level-9-in-game-dev-3-step-3-enemies-level-help/35722/4 "2023-06-08T13:12:41Z")

</div>

> [@Cong\_Vinh\_Tran](#):
>
> `onUpdateOgre(unit)`

Maybe surround this with an  
`if unit:`?

---

<div class="post-metadata">

**Author:** ![Cong\_Vinh\_Tran](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cong_vinh_tran/32/34408_2.png) [@Cong\_Vinh\_Tran](https://discourse.codecombat.com/u/Cong_Vinh_Tran)\
**Post date:** [June 8, 2023, 2:23pm UTC](https://discourse.codecombat.com/t/i-am-stuck-at-level-9-in-game-dev-3-step-3-enemies-level-help/35722/5 "2023-06-08T14:23:36Z")

</div>

Still doesn’t work. I tried it still said cannot read propety health of null.  
Here is my newest code:

```python
# Step 3 of the Run and Dodge game tutorial.

# ENVIRONMENT
game.spawnXY("forest", -4, 64)
game.spawnXY("forest", 4, 64)
game.spawnXY("forest", 12, 64)
game.spawnXY("forest", 20, 64)
game.spawnXY("forest", 28, 64)
game.spawnXY("forest", 36, 64)
game.spawnXY("forest", 44, 64)
game.spawnXY("forest", 52, 64)
game.spawnXY("forest", 60, 64)
game.spawnXY("forest", 68, 64)
game.spawnXY("forest", 76, 64)
game.spawnXY("forest", -4, 4)
game.spawnXY("forest", 4, 4)
game.spawnXY("forest", 12, 4)
game.spawnXY("forest", 20, 4)
game.spawnXY("forest", 28, 4)
game.spawnXY("forest", 36, 4)
game.spawnXY("forest", 44, 4)
game.spawnXY("forest", 52, 4)
game.spawnXY("forest", 60, 4)
game.spawnXY("forest", 68, 4)
game.spawnXY("forest", 76, 4)

def onUpdateStatic(event):
    thing = event.target
    thing.pos.x -= 0.6
    if thing.pos.x < -4:
        if thing.type == "forest":
            thing.pos.x = 84
        else:
            thing.destroy()

game.setActionFor("forest", "update", onUpdateStatic)

def spawnRandomY(type):
    y = game.randomInteger(12, 56)
    spawn = game.spawnXY(type, 80, y)
    spawn.on("update", onUpdateStatic)

def spawnFences():
    spawnNumber = 1 + (game.time / 10)
    while spawnNumber >= 1:
        spawnRandomY("fence")
        spawnNumber -= 1

# GAME SETTINGS
game.spawnFenceTime = 0
game.spawnGemTime = 0.5
# One more spawn timer, for ogres.
game.spawnOgreTime = 1
game.score = 0
game.topScore = db.get("topScore") or 0
ui.track(game, "time")
ui.track(game, "score")
ui.track(game, "topScore")
goal = game.addManualGoal("Survive at least 20 seconds.")

# PLAYER
player = game.spawnPlayerXY("captain", 20, 34)
player.maxSpeed = 20

def onCollect(event):
    item = event.other
    if item.type == "gem":
        game.score += 5

def onCollide(event):
    unit = event.target
    other = event.other
    if other.type == "fence":
        unit.defeat()

def onDefeatPlayer():
    if game.time > 20:
        game.setGoalState(goal, True)
    else:
        game.setGoalState(goal, False)
    setTopScore()

player.on("collect", onCollect)
player.on("collide", onCollide)
player.on("defeat", onDefeatPlayer)

# ENEMIES
# This handles defeated ogres.
def onDefeatOgre(event):
    unit = event.target
    game.score += 10
    # Defeated ogres should move to the left, just like other environmental objects.
    unit.on("update", onUpdateStatic)

# This updates the positions of the ogres so they slowly catch up to the player.
def onUpdateOgre(event):
    unit = event.target
    # Only update position for un-defeated ogres, who have positive health.
    if unit.health > 0:
        # Set the unit closer to the player. We use the baseX property to track this, because the default ogre behavior might also change it's pos.x property, so we can't use that directly.
        unit.baseX += 0.1
        # If unit.baseX greater than 18:
        if unit.baseX > 18:
            # Set unit.baseX to 18:
            unit.baseX = 18
        # Set unit.pos.x to unit.baseX:
        unit.pos.x = unit.baseX

def spawnOgre():
    y = game.randomInteger(12, 56)
    unit = game.spawnXY("scout", 0, y)
    unit.baseX = 0
    
    # Assign onUpdateOgre to handle the "update" event on unit:
    if unit:
        onUpdateOgre(unit)
    # Assign onDefeatOgre to handle the "defeat" event on unit:
    if unit:
        onDefeatOgre(unit)
    
    unit.on("collide", onCollide)
    unit.behavior = "AttacksNearest"

# GAME LOOP
def checkPlayer():
    if game.time == 20:
        player.say("Win! Bonus time!")
    player.pos.x = 20

def setTopScore():
    topScore = db.get("topScore") or 0
    if game.score > game.topScore:
        db.set("topScore", game.score)

def checkSpawns():
    if game.time > game.spawnFenceTime:
        spawnFences()
        game.spawnFenceTime += 1
    if game.time > game.spawnGemTime:
        spawnRandomY("gem")
        game.spawnGemTime += 1
    if game.time > game.spawnOgreTime:
        spawnOgre()
        game.spawnOgreTime += 2

def onUpdateGame(event):
    checkPlayer()
    checkSpawns()
    game.score += event.deltaTime

game.on("update", onUpdateGame)

```

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/4/5/45044c18890121457017ea8a838c477443831557.jpeg)

---

<div class="post-metadata">

**Author:** ![Cong\_Vinh\_Tran](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cong_vinh_tran/32/34408_2.png) [@Cong\_Vinh\_Tran](https://discourse.codecombat.com/u/Cong_Vinh_Tran)\
**Post date:** [June 8, 2023, 2:37pm UTC](https://discourse.codecombat.com/t/i-am-stuck-at-level-9-in-game-dev-3-step-3-enemies-level-help/35722/6 "2023-06-08T14:37:53Z")

</div>

Never mind, I beated that level

---

<div class="post-metadata">

**Author:** ![MarmiteOnToast](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/marmiteontoast/32/46649_2.png) [@MarmiteOnToast](https://discourse.codecombat.com/u/MarmiteOnToast)\
**Post date:** [June 8, 2023, 2:39pm UTC](https://discourse.codecombat.com/t/i-am-stuck-at-level-9-in-game-dev-3-step-3-enemies-level-help/35722/7 "2023-06-08T14:39:37Z")

</div>

Ah, alright  
Sorry I couldn’t help.

(Again, please mark any post (including one of your own, if you want) as a solution so that the topic closes)
