title: How can I fix "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" in Swift
body: <p>I am trying to add music to a game I created. But I am getting the error:</p>
  
  <blockquote>
    <p>"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.</p>
  </blockquote>
  
  <p>I found another post on Stack Overflow (<a href="https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu">What does &quot;fatal error: unexpectedly found nil while unwrapping an Optional value&quot; mean?</a>), but I do not understand how this applies to my code.</p>
  
  <p>This is my code:</p>
  
  <pre><code>import UIKit
  import SpriteKit
  import GameplayKit
  import AVFoundation
  
  class GameViewController: UIViewController {
      var player:AVAudioPlayer = AVAudioPlayer()
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          do {
              let audioPath = Bundle.main.path(forResource: "HomeSoundtrack", ofType: "m4a")
              try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
          }
          catch {
          }
  
          let session = AVAudioSession.sharedInstance()
  
          do {
              try session.setCategory(AVAudioSessionCategoryPlayback)
          }
          catch {
          }
  
          player.play()
  
          if let view = self.view as! SKView? {
              // Load the SKScene from 'GameScene.sks'
              if let scene = SKScene(fileNamed: "GameScene") {
                  // Set the scale mode to scale to fit the window
                  scene.scaleMode = .aspectFill
  
                  // Present the scene
                  view.presentScene(scene)
              }
  
              view.ignoresSiblingOrder = true
  
              view.showsFPS = false
              view.showsNodeCount = false
          }
      }
  
      override var shouldAutorotate: Bool {
          return true
      }
  
      override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
          if UIDevice.current.userInterfaceIdiom == .phone {
              return .allButUpsideDown
          } else {
              return .all
          }
      }
  }
  </code></pre>