SceneKit – Fatal Error: unexpectedly found nil while unwrapping an Optional value when getting child node

LinusGeffarth

So I was trying to use a 3D object (.dae 1.4, exported with Cinema4D r13) in my SceneKit/Swift project. I left the provided code and just changed:

let scene = SCNScene(named: "art.scnassets/ship.dae")!

to fit mine:

let scene = SCNScene(named: "art.scnassets/pyramid.dae")!

I did also import the file into the project where it is supposed to be (in the art.scnassets). I can preview the file just like the default ship so that should be correct.

I also changed:

let ship = scene.rootNode.childNodeWithName("ship", recursively: true)!

to fit mine again:

let pyramide = scene.rootNode.childNodeWithName("pyramid", recursively: true)!

When I now run the project then it crashes and gives me the following error in the second line I modified.

fatal error: unexpectedly found nil while unwrapping an Optional value

I saw this error in some other posts already but it didn't actually fit my case and did not help me either.

Does anyone have an idea how to solve this?

NOTE

I created the same project but with Obj-C instead of Swift and it does not complain but only shows me this: (yes, I made sure it's coordinates are 0,0,0 and no, its not the color)

iOS Simulator output

rickster

You probably don't have a node named pyramid in your DAE file, so the childNodeWithName is returning nil, and since you're force-unwrapping that return value you crash.

You can look at that file in Xcode and examine node names there to make sure you have the name right.

Also, you might consider restructuring your code to report meaningful errors when your find a nil you aren't expecting:

if let pyramid = scene.rootNode.childNodeWithName(...) {
    ...
} else {
    fatalError("missing pyramid mode in scene file")
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Related Related

HotTag

Archive