Fatal error: Unexpectedly found nil while unwrapping an Optional value

Hi dev', i have got a " Fatal error: Unexpectedly found nil while unwrapping an Optional value" on my code. Can you help me please?




/

/

/

/

/

/

/

import UIKit

import SceneKit

import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

@IBOutlet var sceneView: ARSCNView!


func addsiteWeb(x: Float = 0, y: Float = 0, z: Float = -0.5) {

guard let siteWebScene = SCNScene(named: "SceneKit Scene.scn"), let siteWebNode = siteWebScene.rootNode.childNode(withName: "siteweb", recursively: true) else { return }

siteWebNode.position = SCNVector3(x, y, z)

sceneView.scene.rootNode.addChildNode(siteWebNode)

}

override func viewDidLoad() {

super.viewDidLoad()

addsiteWeb()

/

sceneView.delegate = self

/

sceneView.showsStatistics = true

sceneView.debugOptions = ARSCNDebugOptions.showWorldOrigin

/

let scene = SCNScene(named: "SceneKit Scene.scn")! HERE please

/

sceneView.scene = scene

}

override func viewWillAppear(_ animated: Bool) {

super.viewWillAppear(animated)

/

let configuration = ARWorldTrackingConfiguration()

/

sceneView.session.run(configuration)

}

override func viewWillDisappear(_ animated: Bool) {

super.viewWillDisappear(animated)

/

sceneView.session.pause()

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

/

}

/

/

/

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {

let node = SCNNode()

return node

}

*/

func session(_ session: ARSession, didFailWithError error: Error) {

/

}

func sessionWasInterrupted(_ session: ARSession) {

/

}

func sessionInterruptionEnded(_ session: ARSession) {

/

}

}

Most likely : the resource

SceneKit Scene.scn

does not exist.


And so, SCNScene(named:) return nil

When you try to unwrap, just crash.


Check that the name matches perfectly, including spaces, uppercases…


You could have the same problem in


    func addsiteWeb(x: Float = 0, y: Float = 0, z: Float = -0.5) {
        guard let siteWebScene = SCNScene(named: "SceneKit Scene.scn"), let siteWebNode = siteWebScene.rootNode.childNode(withName: "siteweb", recursively: true) else { return }
        siteWebNode.position = SCNVector3(x, y, z)
        sceneView.scene.rootNode.addChildNode(siteWebNode)
    }


And then, position would not be initialized.


You can test with:


guard let siteWebScene = SCNScene(named: "SceneKit Scene.scn"), let siteWebNode = siteWebScene.rootNode.childNode(withName: "siteweb", recursively: true) else { print("webScene not initialized") ; return }

Thanks a lot for your answer. I checked the name of my files and it's the same. i tried to change the name of the file but it didn't change anything.



I used your code and the console return : "webScene not initialized"


Any idea please? 😕

https://www.noelshack.com/2018-10-5-1520597331-capture-d-ecran-2018-03-09-a-13-08-18.png


Here you can see my Xcode page.


(sorry for the double post)

That just confirms that the scene cannot be found.


If you are absolutely sure this exact resource is in your app bundle, try to remove the .scn extension :


let scene = SCNScene(named: "SceneKit Scene")!

I'm absolutely sure. I tried to remove .scn extention but always the same error. Where can i send you a screenshot of my Xcode page?


I think for you it's very easy but for me it's like to run 12 hours without water aha. Sorry for my beginner level.

My SceneKit Scene.scn on the left of xcode was on "?" I change control to "A" (add) but i always have the same error.

You visibly have an issue for accessing the resource.


I'm not familiar with sceneKit, but you could :

1. Do a clean build folder (option clean in Product menu).


2. If that does not work, if possible, I would delete the resource (scene) and recreate with the same name.

I have the same problem with Xcode 13.2.1: I have many .scn files and none seems to work. However, at compilation time I get the warning

Could not find bundle inside /Library/Developer/CommandLineTools scntool

the bundle indeed seems to be missing. It was working with Xcode 11, about one and a half year ago

Fatal error: Unexpectedly found nil while unwrapping an Optional value
 
 
Q