When I switch to my gamescene from my menu scene, it works perfectly fine. But, when I try to go back, it is a disaster. I get: unexpectedly found nil while unwrapping an Optional value. The code for going back is:
let menuscene = MenuScene(size: self.size)
menuscene.scaleMode = scaleMode
self.view?.presentScene(menuscene)
The code that it says the error comes from is:
PlaygamebtnNode = self.childNode(withName: "PlaygamebtnNode") as! SKSpriteNode
NamelblNode = self.childNode(withName: "NamelblNode") as! SKLabelNode
This is odd because when the game launches, it is fine with this code. The error has to come from the change in views/scenes. The code for the entrie MenuScene class is:
import SpriteKit
import GameplayKit
class MenuScene: SKScene {
var PlaygamebtnNode:SKSpriteNode!
var NamelblNode:SKLabelNode!
override func didMove(to view: SKView) {
PlaygamebtnNode = self.childNode(withName: "PlaygamebtnNode") as! SKSpriteNode
NamelblNode = self.childNode(withName: "NamelblNode") as! SKLabelNode
PlaygamebtnNode.texture = SKTexture(imageNamed: "Playgamebtn")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in: self){
let nodesArray = self.nodes(at: location)
if nodesArray.first?.name == "PlaygamebtnNode" {
let transition = SKTransition.flipHorizontal(withDuration: 0.5)
let nextScene = GameScene(size: self.size)
self.view?.presentScene(nextScene, transition: transition)
}
}
}
}
The reason this is not a ordinary found nil while unwrapping an Optional value question is because it does not show this error until I try to go back to the menu.