Simple RealityKit app crashes when loading larger model

This code works perfectly on a simple cube, however when loading a larger (250mb) .rcproject file I get the "Unexpectedly found nil while implicitly unwrapping an Optional value" error.

My code is as follows:

struct ARSandBox: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {

    var map: ModelEntity!

    let arView = ARView(frame: .zero)
    let delayTime = DispatchTime.now() + 3.0

    let scene = try! TeaTestMap.loadMapScene()

    map = scene.theMap!.children[0] as? ModelEntity

(Error is here at map.isEnabled)

    map.isEnabled = false
    arView.scene.anchors.append(scene)

    DispatchQueue.main.asyncAfter(deadline: delayTime) {
        map.isEnabled = true
    }

This seems to indicate that your Implicitly Unwrapped Optional variable, map, did not successfully cast as ModelEntity, and is therefore nil when you try to access it.

Try printing scene.theMap!.children[0] to see if you can discern what its type is, and then re-examine your assumption that it is a ModelEntity.

Hi! Yes, follow gchiste's advice above. Also, if you are unable to figure this out, please send us your sample project via Feedback Assistant, and post the feedback ID here.

Try using HasModel instead of ModelEntity.

Your best bet at solving this is putting a breakpoint after setting map and having a look at the contents with Xcode's inspector.

Simple RealityKit app crashes when loading larger model
 
 
Q