Fatal errorUnexpectedly found nil while unwrapping an Optional value

I suspect this has something to do with the new XCode update.

After updating to XCode 13, my app now gives me the error of Fatal errorUnexpectedly found nil while unwrapping an Optional value

The error comes from the first line:

     let scene = SCNScene(named: "art.scnassets/Pokeball.scn")!

    let ship = scene.rootNode.childNode(withName: "pokeball", recursively: false)!
    ship.position = SCNVector3(0, 0, -0.3)
    sceneView.scene = scene!

I did not modify the code so I have no idea why it is no longer functional. I double checked the file path and names are correct, I swapped to the default ship model file and still fails. What do I need to change to fix this?

Answered by robnotyou in 688533022

You are force-unwrapping 3 Optionals, and you say the error comes from the first line.

• SCNScene(named: "art.scnassets/Pokeball.scn")!
• scene.rootNode.childNode(withName: "pokeball", recursively: false)!
• scene!

"scene!" doesn't make sense, since "scene" is not an Optional... I would expect this to give you a compiler error.

I suggest you try some simple diagnostics, like:

if let scene = SCNScene(named: "art.scnassets/Pokeball.scn") {
    print("got scene: art.scnassets/Pokeball.scn")
} else {
    print("couldn't find scene: art.scnassets/Pokeball.scn")
}
Accepted Answer

You are force-unwrapping 3 Optionals, and you say the error comes from the first line.

• SCNScene(named: "art.scnassets/Pokeball.scn")!
• scene.rootNode.childNode(withName: "pokeball", recursively: false)!
• scene!

"scene!" doesn't make sense, since "scene" is not an Optional... I would expect this to give you a compiler error.

I suggest you try some simple diagnostics, like:

if let scene = SCNScene(named: "art.scnassets/Pokeball.scn") {
    print("got scene: art.scnassets/Pokeball.scn")
} else {
    print("couldn't find scene: art.scnassets/Pokeball.scn")
}

Sorry the previous code was the old version, the rest of the lines are not suppose to have ! Anyway I tried and indeed found that it is complaining the scene is not found from the console output.

couldn't find scene: art.scnassets/Pokeball.scn

How can it failed to find the file?

I also use xcode13.2.1 version and select iOS >Game >SceneKit to create and build the project, but I get the same error. Is this a problem with xcode13.2.1 version?

Fatal errorUnexpectedly found nil while unwrapping an Optional value
 
 
Q