[SOLVED] Cannot read property 'isParent' of undefined

I am currently working on a mini game. 90% of the time everything works as intended. I sometimes get the error :

World.js:1855 Uncaught TypeError: Cannot read property 'isParent' of undefined at World.collideObjects (World.js:1855) at World.collide (World.js:1832) at Scene.update (index.js:387) at Systems.step (Systems.js:371) at SceneManager.update (SceneManager.js:562) at Game.step (Game.js:474) at TimeStep.step (TimeStep.js:558) at step (RequestAnimationFrame.js:105)

My code

function update () { if (startGame) { dragonFlapShadow.visible = true dragonFlapShadow.setPosition(dragonFlap.x + 60, dragonFlap.y + 240) }

this.physics.world.collide(killZone, wallCenter, atkCollision)

this.physics.world.collide(killZone, noKillZone, noAtkCollision)

killZone.setPosition(dragonFlap.x + 120, dragonFlap.y)

killZone.body.debugBodyColor = killZone.body.touching.none ? 0x35e0ff : 0xff1938

if (dragAtk && canAtk && startGame) {
    dragonFire.visible = true
    dragonFire.setPosition(dragonFlap.x, dragonFlap.y)

} else if (!dragAtk && startGame && canAtk) {
    dragonFire.visible = false
    wallCenter.tint = 0xffffff
    wallCenter.body.debugBodyColor = 0x35e0ff
    canAtk = false
}

if (dragMoveTo) {
    this.physics.moveToObject(dragonFlap, target, 600, 200)
    dragAtk = false
}

let distance = Phaser.Math.Distance.Between(dragonFlap.x, dragonFlap.y, target.x, target.y)
if (dragonFlap.body.speed > 0)
{
    if (distance < 4)
    {
        dragonFlap.body.reset(target.x, target.y)
        dragMoveTo = false
        dragAtk = false
    }
}

if (canAtk && dragAtk) {
    wallCenter.body.debugBodyColor = 0x004646
    wallCenter.tint = 0x32d6ff
    killZone.body.debugBodyColor = 0xff1938
    wallDamage = true
    wallDmg()
} else {
    dragAtk = false
    wallCenter.body.debugBodyColor = 0x35e0ff
    wallCenter.tint = 0xffffff
    killZone.body.debugBodyColor = 0x35e0ff
    dragonFire.visible = false
    wallDamage = false
    wallCanShrink = false
    //console.log('no overlap')
}

// SET WALL SCALE
wallCenter.setScale(wallScale)

// WALL CAN SHRINK IF BEING DAMAGED
if (wallDamage) {
    wallShrink.paused = false 
    wallCanShrink = true
} else {
    wallShrink.paused = true 
    wallCanShrink = false
}

// DAMAGE THE WALL
if (camShake) {
    this.cameras.main.shake(shakeAmt, 0.005)
    camShake = false
    console.log('wall HP ' + wallHealth)
    console.log('wall scale ' + wallScale)
}

}`

I’m assuming it’s a scope issue but I’m not 100% sure. ‘isParent’ is confusing to me because I would assume I would get this error when dealing with groups. What’s odd to me is that the error doesn’t happen all the time. I’ve tried to change the order of operations - but then I randomly get undefined errors with this line killZone.setPosition(dragonFlap.x + 120, dragonFlap.y) Where it seems to choke on the .x or .y positioning.

For context, I am using the https://github.com/Quinten/phaser-3-assets-as-data-uri for a single html file approach.

Any help would be greatly appreciated.

I resolved the issue by putting everything in my update function in an if statement

 function update () {
      if (startGame) {
        
      }
 }

Not sure as to why this is. But it’s working.