Why will updateScore function in viewcontroller not call from gamescene

So I'm trying to make it so an ad will pop up every time the score in game scene reaches a certain point in my game in Xcode using Swift but it's not calling the function that does that(updateViewScore), I don't know if I'm missing something to call the function from gamescene when the function itself is in viewcontroller. I'm new to this and it's my first time making a game so what am I doing wrong? I made the functions and code below and call updateViewScore in gamescene when the score increases but updateScore never gets called as the message isn't printed no matter how much the score increases.

//in gamescene
score += 1  // Add this line to increase the score
updateViewScore()

func updateViewScore() {
       
viewController?.updateScore(score: 10)
        
}

//in viewcontroller
var gameScene: GameScene!
gameScene = GameScene(size: view.bounds.size)
gameScene.viewController = self

func updateScore(score: Int) {
    
print("updateScore was called with score: \(score)")
   
popUp(score)

}

@IBAction func popUp(_ sender: Any) {

if score % 10 == 0 {
    if interstitial != nil {
        interstitial?.present(fromRootViewController: self)
    } else {
        print("Ad wasn't ready")
    }
}

}

Upvotes: 0

Views: 26

Answers (1)

mervesenayalcinkaya
mervesenayalcinkaya

Reputation: 1

The code can work if you call a single function instead of calling functions within functions. Since I haven't written that many nested functions, it occurred to me that this might be the problem.

Upvotes: 0

Related Questions