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)
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) } }
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)
Do not force unwrapping, because if it's fail the app will crash.
Hope this resolve 😎
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 😎