# Error when setting up a node for a 3D sphere

**URL:** <https://forums.swift.org/t/error-when-setting-up-a-node-for-a-3d-sphere/53625>\
**Category:** Using Swift\
**Tags:** videogames\
**Created:** [November 20, 2021, 11:42pm UTC](https://forums.swift.org/t/error-when-setting-up-a-node-for-a-3d-sphere/53625 "2021-11-20T23:42:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kat9265](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@kat9265](https://forums.swift.org/u/kat9265)\
**Post date:** [November 20, 2021, 11:42pm UTC](https://forums.swift.org/t/error-when-setting-up-a-node-for-a-3d-sphere/53625/1 "2021-11-20T23:42:12Z")

</div>

I'm writing code for a 3d ball to move when the user clicks it with its mouse. I'm having trouble at one line of my code. When I try to set up the node of the ball it gives me the error "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value". On this line: ballNode = scene.rootNode.childNode(withName: "Ball", recursively: true)! I have never gotten this error before and I don't see how my code be wrong.

```swift
import UIKit
import SceneKit

class GameViewController: UIViewController {
    
    let CategoryLamp = 2
    
    var sceneView:SCNView!
    var scene:SCNScene!
    
    var ballNode:SCNNode!
    var selfieStickNode:SCNNode!
    
    var motion = MotionHelper()
    var motionForce = SCNVector3(0, 0, 0)
    
    var sounds:[String:SCNAudioSource] = [:]
    
    override func viewDidLoad() {
        setupScene()
        setupNodes()
    }
    
    func setupScene(){
        sceneView = self.view as! SCNView
        sceneView.delegate = self
        
        //sceneView.allowsCameraControl = true
        scene = SCNScene(named: "art.scnassets/MainScene.scn")
        sceneView.scene = scene
        
        scene.physicsWorld.contactDelegate = self
        
        let tapRecognizer = UITapGestureRecognizer()
        tapRecognizer.numberOfTapsRequired = 1
        tapRecognizer.numberOfTouchesRequired = 1
        
        tapRecognizer.addTarget(self, action: #selector(GameViewController.sceneViewTapped(recognizer:)))
        sceneView.addGestureRecognizer(tapRecognizer)
        
    }
    
    func setupNodes() {
        ballNode = scene.rootNode.childNode(withName: "Ball", recursively: true)! // Where the error is occurring 
        ballNode.physicsBody?.contactTestBitMask = CategoryLamp
        selfieStickNode = scene.rootNode.childNode(withName: "selfieNode", recursively: true)!
    }
    
    
    
    @objc func sceneViewTapped (recognizer:UITapGestureRecognizer) {
        let location = recognizer.location(in: sceneView)
        let hitResults = sceneView.hitTest(location, options: nil)
        
        if hitResults.count > 0 {
            let result = hitResults.first
            if let node = result?.node {
                if node.name == "Ball" {
                    ballNode.physicsBody?.applyForce(SCNVector3(x: 0, y:4, z: -2), asImpulse: true)
                }
            }
        }
        
        
    }
    
    
    override var shouldAutorotate: Bool {
        return false
    }
    
    override var prefersStatusBarHidden: Bool {
        return true
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

extension GameViewController : SCNSceneRendererDelegate {
    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
        let ball = ballNode.presentation
        let ballPosition = ball.position
        
        let targetPosition = SCNVector3(x: ballPosition.x, y: ballPosition.y + 5, z:ballPosition.z + 5)
        var cameraPosition = selfieStickNode.position
        
        let camDamping:Float = 0.3
        
        let xComponent = cameraPosition.x * (1 - camDamping) + targetPosition.x * camDamping
        let yComponent = cameraPosition.y * (1 - camDamping) + targetPosition.y * camDamping
        let zComponent = cameraPosition.z * (1 - camDamping) + targetPosition.z * camDamping
        
        cameraPosition = SCNVector3(x: xComponent, y: yComponent, z: zComponent)
        selfieStickNode.position = cameraPosition
        
        
        motion.getAccelerometerData { (x, y, z) in
            self.motionForce = SCNVector3(x: x * 0.05, y:0, z: (y + 0.8) * -0.05)
        }
        
        ballNode.physicsBody?.velocity += motionForce
        
    }
    
    
}

extension GameViewController : SCNPhysicsContactDelegate {
    func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
        var contactNode:SCNNode!
        
        if contact.nodeA.name == "Ball" {
            contactNode = contact.nodeB
        }else{
            contactNode = contact.nodeA
        }
        
        if contactNode.physicsBody?.categoryBitMask == CategoryLamp {
            contactNode.isHidden = true
            
            let waitAction = SCNAction.wait(duration: 15)
            let unhideAction = SCNAction.run { (node) in
                node.isHidden = false
            }
            
            let actionSequence = SCNAction.sequence([waitAction, unhideAction])
            
            contactNode.runAction(actionSequence)
        }
        
    }
    
    
}

```

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [November 21, 2021, 2:39am UTC](https://forums.swift.org/t/error-when-setting-up-a-node-for-a-3d-sphere/53625/2 "2021-11-21T02:39:50Z")

</div>

> [@kat9265](#):
>
> Unexpectedly found nil while unwrapping an Optional value

This indicates that you do not have a node named "Ball". Where do you create this node?

---

<div class="post-metadata">

**Author:** ![kat9265](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@kat9265](https://forums.swift.org/u/kat9265)\
**Post date:** [November 21, 2021, 11:29am UTC](https://forums.swift.org/t/error-when-setting-up-a-node-for-a-3d-sphere/53625/3 "2021-11-21T11:29:52Z")

</div>

I create the node in the art folder and I have named it “Ball”. I have then put this ball in a scene called MainScene that is also in the art folder
