CHiMPS
CHiMPS

Reputation: 3

"unexpectedly found nil while unwrapping an Optional value" when calling from another scene

I created the GameScene first and it works, now I'm trying to add a MainMenu scene which also works except when I hit the button(sprite) to start game, then I get the error above.

The GameViewController is just stock apple code and it starts the app with the MainMenu scene, but I've tested the app just running GameScene in GameViewController and I don't get any error but then i'll have no mainMenu

Within GameScene:

var UpperLeft = SKSpriteNode()
var BottomRight = SKSpriteNode()
var UpperRight = SKSpriteNode()
var BottomLeft = SKSpriteNode()
var Ball = SKSpriteNode()

the error is with each one i'm forcing to unwrap ↓

    override func didMove(to view: SKView) {
    scoreLabel = self.childNode(withName:"scoreLabel") as! SKLabelNode
    UpperLeft = self.childNode(withName:"UpperLeft") as! SKSpriteNode
    BottomRight = self.childNode(withName:"BottomRight") as! SKSpriteNode
    UpperRight = self.childNode(withName:"UpperRight") as! SKSpriteNode
    BottomLeft = self.childNode(withName:"BottomLeft") as! SKSpriteNode
    Ball = self.childNode(withName:"Ball") as! SKSpriteNode

The error is with each one i'm forcing to unwrap ↑

Within MainMenu:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    super.touchesBegan(touches, with: event)

    if let location = touches.first?.location(in: self) {
        let touchedNode = atPoint(location)

        if touchedNode.name == "StartGame" {

            let transition = SKTransition.reveal(with: .down, duration: 1.0)

           let nextScene = GameScene(size: scene!.size)
           nextScene.scaleMode = .aspectFill

            scene?.view?.presentScene(nextScene, transition: transition)
        }
    }
}

Within GameViewController:

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()


    if let view = self.view as! SKView? {
        // Load the SKScene from 'GameScene.sks'
        if let scene = SKScene(fileNamed: "mainMenu") {
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill

            view.ignoresSiblingOrder = true
            // Present the scene


            view.presentScene(scene)
        }

        view.ignoresSiblingOrder = true

        view.showsFPS = true
        view.showsNodeCount = true
    }
}

I understand that the error comes from the as! forcing the unwrap but I don't see how calling the GameScene from the mainMenu scene causes this to be an issue? Because I did create an object for each unwrap so they should not be nil?

Upvotes: 0

Views: 820

Answers (1)

Mina
Mina

Reputation: 2212

The problem is in the way you initialize GameScene

use the following code:

 let nextScene = GameScene(fileNamed: "GameScene")

instead of the one used in MainMenu:

 let nextScene = GameScene(size: scene!.size)

when you use size to initialize a scene, it doesn't recognize the .sks file of the scene, so all your nodes which defines in .sks file become nil and that's why you get error while unwrapping them.

Upvotes: 2

Related Questions