Help needed. Fatal error:Unexpectedly found nil while unwrapping an Optional value

Hey,

Thank you for taking a look at this and for your help in advance.....

I'm new to this so sorry if I haven't posted this is in the correct way.

I am receiving an error on an IF statement in a didBegin() function in my game when I run it. The game builds and runs ok but on occasion it crashes and I get this error. Usually it happens when there are lots of nodes on the screen making contact with other nodes.

The error is on the IF statement line 25 on the code below.

I've been trying to fix this but so far no luck so I'm hoping someone on here with more experience can help me out.

Thank you
MasterChief: Spartan G14

Error message : Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Swift - Xcode version Version 11.1 (11A1027)

Code Block
 func didBegin(_ contact: SKPhysicsContact) {
        var bodyOne:SKPhysicsBody
        var bodyTwo:SKPhysicsBody
        
        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
            bodyOne = contact.bodyA
            bodyTwo = contact.bodyB
        }else{
            bodyOne = contact.bodyB
            bodyTwo = contact.bodyA
        }
        
        if (bodyOne.categoryBitMask & playerWeaponCategory) != 0  && (bodyTwo.categoryBitMask & enemyCategory) != 0 {
                    playerWeaponHitEnemyUnit(playerWeaponAsset: bodyOne.node as! SKSpriteNode, enemyAsset: bodyTwo.node as! SKSpriteNode)
                 }
        
    }




 
Answered by VictorAbroum in 615687022
Hey MasterchiefG14, I think the error is in the line 27, where you force an unwrapping trying to cast this " bodyOne.node as! SKSpriteNode"

Try make an if let statement

playerWeaponHitEnemyUnit(playerWeaponAsset: bodyOne.node as! SKSpriteNode, enemyAsset: bodyTwo.node as! SKSpriteNode)

Code Block swift
if let spriteBodyOne = bodyOne.node as? SKSpriteNode,
let spriteBodyTwo = bodyTwo.node as? SKSpriteNode {
playerWeaponHitEnemyUnit(playerWeaponAsset: spriteBodyOne, enemyAsset: spriteBodyTwo)
}


Do not force unwrapping, because if it's fail the app will crash.

Hope this resolve 😎

We need to find which optional is nil.
Please add a test line 23:

Code Block
print("bodyOne.categoryBitMask", bodyOne.categoryBitMask)
print("playerWeaponCategory", playerWeaponCategory)
print("bodyTwo.categoryBitMask", bodyTwo.categoryBitMask)
print("enemyCategory", enemyCategory)

Please also show how you declared playerWeaponCategory and enemyCategory and how you set them.

And tell what you get.

Accepted Answer
Hey MasterchiefG14, I think the error is in the line 27, where you force an unwrapping trying to cast this " bodyOne.node as! SKSpriteNode"

Try make an if let statement

playerWeaponHitEnemyUnit(playerWeaponAsset: bodyOne.node as! SKSpriteNode, enemyAsset: bodyTwo.node as! SKSpriteNode)

Code Block swift
if let spriteBodyOne = bodyOne.node as? SKSpriteNode,
let spriteBodyTwo = bodyTwo.node as? SKSpriteNode {
playerWeaponHitEnemyUnit(playerWeaponAsset: spriteBodyOne, enemyAsset: spriteBodyTwo)
}


Do not force unwrapping, because if it's fail the app will crash.

Hope this resolve 😎

Thanks for the replies and help.

@VictorAbroum - you are an absolute legend. This has worked perfectly! - It's now fixed and the game is no longer crashing

Thank you for this.
Help needed. Fatal error:Unexpectedly found nil while unwrapping an Optional value
 
 
Q