1

My Game is in the GameScene and Menu in my MainMenu Scene.

I put the following code in the overridden touchesBegan of the MainMenu Scene to present the GameScene:

   let reveal = SKTransition.fade(withDuration: 0.5)
   let NextScene = GameScene(size: self.size)
   NextScene.scaleMode = SKSceneScaleMode.aspectFill
   self.view?.presentScene(NextScene, transition:reveal)

But if I test it I get the following error:

fatal error: unexpectedly found nil while unwrapping an Optional value

I just don't understand why because in the GameScene I have the follow code in the GameOver function which works perfectly fine:

    let reveal = SKTransition.fade(withDuration: 0.5)
    let MainMenuScene = MainMenu(size: self.size)
    MainMenuScene.scaleMode = SKSceneScaleMode.aspectFill
    self.view?.presentScene(MainMenuScene, transition:reveal)

They are the exact same but only the one in the GameOver Function works, why?

Confused
  • 6,048
  • 6
  • 34
  • 75
Lukas
  • 451
  • 2
  • 13
  • It would helpful to see the line where you are doing a hard unwrapping of a nil value. – OwlOCR Jan 29 '17 at 19:27
  • How do i find the line? it doesn't show me a red warning sign or something – Lukas Jan 29 '17 at 19:28
  • When it crashes on EXC_BAD_INSTRUCTION Xcode should display the offending line in red, at least if its in your code. You would also see the call stack in the left hand bar (Debug Navigator). – OwlOCR Jan 29 '17 at 19:33
  • When it crashes it just goes to a different window which is called Thread 1, and then theres a message in the last line saying: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1005c51fc). And i can't find any red line in my code after it crashes. – Lukas Jan 29 '17 at 19:38
  • try to call these four lines using nested if like: if let reveal = SKTransition.fade(withDuration: 0.5){ if let NextScene = GameScene(size: self.size){ NextScene.scaleMode = SKSceneScaleMode.aspectFill self.view?.presentScene(NextScene, transition:reveal)}} .. will prevent you to the crash. – vaibhav Jan 29 '17 at 20:13
  • Both if statements have a Error: "Initializer for conditional binding must have Optional type, not "SKTransition"" and "Initializer for conditional binding must have Optional type, not "GameScene"". And if i put brackets like this: if (let reveal = SKTransition.fade(withDuration: 0.5)){ theres also an Error – Lukas Jan 29 '17 at 20:26
  • Are you sure that you spelled your class (scene) names correctly? Maybe you made a spelling error/typo somewhere. I would suspect that the mistake would be when you initially choose the name of the scene – Nik Jan 30 '17 at 01:40
  • Just put an execption breakpoint to see what line causes a crash. And be careful with forced unwrapping as well as forced downcasting. – Whirlwind Jan 30 '17 at 08:21
  • I think there could be a forced assignment in your GameScene to `didMoveTo` method or inside this class..you should investigate there.. – Alessandro Ornano Jan 30 '17 at 08:30
  • It seems like there's something wrong with: player = self.childNode(withName: "player") as! SKSpriteNode, but i don't know what. – Lukas Jan 30 '17 at 16:33
  • any ideas what wrong with it? – Lukas Jan 30 '17 at 18:44

0 Answers0