Search code examples
iosswiftxcodearkit

Xcode ARKit project: Fatal error: Unexpectedly found nil while unwrapping an Optional value


I have noticed, that when I try to create a AR project on IOS with Xcode, even that if I use default settings and default project - I receive this kind of the "Fatal Error":

//
//  ViewController.swift
//  test
//
//

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Set the view's delegate
        sceneView.delegate = self
        
        // Show statistics such as fps and timing information
        sceneView.showsStatistics = true
        
        // Create a new scene
        let scene = SCNScene(named: "art.scnassets/ship.scn")!
        
        // Set the scene to the view
        sceneView.scene = scene
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // Create a session configuration
        let configuration = ARWorldTrackingConfiguration()

        // Run the view's session
        sceneView.session.run(configuration)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        // Pause the view's session
        sceneView.session.pause()
    }

    // MARK: - ARSCNViewDelegate
    
/*
    // Override to create and configure nodes for anchors added to the view's session.
    func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
        let node = SCNNode()
     
        return node
    }
*/
    
    func session(_ session: ARSession, didFailWithError error: Error) {
        // Present an error message to the user
        
    }
    
    func sessionWasInterrupted(_ session: ARSession) {
        // Inform the user that the session has been interrupted, for example, by presenting an overlay
        
    }
    
    func sessionInterruptionEnded(_ session: ARSession) {
        // Reset tracking and/or remove existing anchors if consistent tracking is required
        
    }
}

As soon as I lunch the app, I get this error in debug:

2023-04-16 02:12:05.471666+0300 test[13308:8472291] [SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)")
2023-04-16 02:12:05.471706+0300 test[13308:8472291] [SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)")
2023-04-16 02:12:05.475942+0300 test[13308:8472291] Metal GPU Frame Capture Enabled
2023-04-16 02:12:05.476043+0300 test[13308:8472291] Metal API Validation Enabled
test/ViewController.swift:26: Fatal error: Unexpectedly found nil while unwrapping an Optional value
2023-04-16 02:12:05.549337+0300 test[13308:8472291] test/ViewController.swift:26: Fatal error: Unexpectedly found nil while unwrapping an Optional value
(lldb) 

But I am 100% sure, that the file is in the folder art.scnassets and path name is correct.


Solution

  • Found the solution. Somehow, this folder is not accessible even if it is the default. What this is what I did and it helped me to launch the app without Fatal Error:

    1. Locate your project and the folder art.scnassets in your project
    2. Drag all items from the folder here (yes, just under the list): enter image description here 3)Confirm this step. enter image description here
    3. Change this part of your code, where you try to access this item:
      OLD code
            // Create a new scene
            let scene = SCNScene(named: "art.scnassets/ship.scn")!
    

    NEW code

            // Create a new scene
            let scene = SCNScene(named: "/ship.scn")!
    
    1. Enjoy.

    P.S. Maybe it is not the most elegant solution, so if you know how to fix it in a better way - please, let me know. In my case, when I create another folder and I try to put it there - it doesn't really help me.

    To solve this problem, I started here: ARKit Project: Unexpectedly found nil while unwrapping an Optional value, so I kinda fixed the problem, but I am not a direct author of this solution.