-1

I am getting the following error on the line basicTop.run(SK...

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

I do not know why I am getting this. I get this error message when I click on the simulator screen.

class GameScene: SKScene {

    var brickSwitch: SKSpriteNode!
    var basicTop: SKSpriteNode!
    var basicBottom: SKSpriteNode!
    var basicLeft: SKSpriteNode!
    var basicRight: SKSpriteNode!

    override func didMove(to view: SKView) {
        layoutScene()
    }

    func turnBasicTop() {
        basicTop.run(SKAction.rotate(byAngle: .pi/2, duration: 0.25))
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        turnBasicTop()
    }
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • 3
    Possible duplicate of: [What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – TylerP Feb 17 '20 at 19:58

1 Answers1

0

Check whether an optional basicTop contains a value,

if basicTop != nil {
    basicTop.run(SKAction.rotate(byAngle: .pi/2, duration: 0.25))
}

Also, please try to read articles before posting basic questions

https://developer.apple.com/documentation/swift/optional

Harish
  • 2,496
  • 4
  • 24
  • 48
  • I changed it to that: func turnBasicTop() { if basicTop != nil { basicTop.run(SKAction.rotate(byAngle: .pi/2, duration: 0.25)) } } The error went away, but when I click on the simulator screen nothing happens. – Christopher Hahn Feb 17 '20 at 20:07
  • this is because the object 'basicTop' doesn't have any value. You have to assign value to basicTop. – Harish Feb 17 '20 at 20:16
  • So what you are saying is that I need to set var BasicTop equal to an image? If so, how do I do that? I am new to programming, so I am sorry for the basic questions. – Christopher Hahn Feb 17 '20 at 20:46
  • class GameScene: SKScene { var brickSwitch: SKSpriteNode! var basicTop = SKSpriteNode(imageNamed: "Top Side"); var basicBottom: SKSpriteNode! var basicLeft: SKSpriteNode! var basicRight: SKSpriteNode! override func didMove(to view: SKView) { layoutScene() } Should all the variables be written like var basicTop – Christopher Hahn Feb 17 '20 at 20:52
  • I changed func turnBasicTop() back to how it was originally, and I am not getting any errors. When I load the simulator, however, the image still doesn't rotate. – Christopher Hahn Feb 17 '20 at 20:57
  • You don’t need that `if` statement. Simply: `basicTop?.run(SKAction.rotate(byAngle: .pi/2, duration: 0.25))` – Rob Feb 17 '20 at 21:24
  • @Rob it says Cannot use optional chaining on non-optional value of type 'SKSpriteNode' – Christopher Hahn Feb 17 '20 at 21:35
  • You declared your property as `var basicTop: SKSpriteNode!`, which is an optional, specifically an implicitly unwrapped optional (IOP). If it's saying that “Cannot use optional chaining on non-optional value” then the code isn't as you’ve outlined in your question (e.g. you must have unwrapped it already). – Rob Feb 17 '20 at 21:40
  • Right now, it is set as var basicTop = SKSpriteNode(imageNamed: "Top Side"); However, the image still does not rotate. – Christopher Hahn Feb 17 '20 at 22:47