SpriteKit childnode(withname: String) return nil

151 views Asked by At

I have created a .sks file for back button. I want to use it in all my scenes. I try to load that back button on my GameScene file but I got "Unexpectedly found nil while unwrapping an Optional value" error. I already fill the name field of the back button as same as I call it on GameScene.

class GameScene: SKScene {

//Back to Level Selection Reference Node
var backButton: SKNode!
var buttonBack: SKSpriteNode!
//Back to Home (Game Selection) Reference Node
var buttonHome: SKSpriteNode!

override func sceneDidLoad() {
    super.sceneDidLoad()
    buttonBack = childNode(withName: "btnBack") as! SKSpriteNode

    buttonHome = childNode(withName: "//btnHome") as! SKSpriteNode
    
}

I got error on line

buttonBack = childNode(withName: "btnBack") as! SKSpriteNode

When I comment this line I get error on the following line. I already fill the name field with "btnBack" under Attributes Inspector.

Any idea to solve this issue?

0

There are 0 answers