Error: Scene is modified within a rendering callback of another scene (). This is not allowed and may lead to crash

I am trying to transition from one scene to another for my swift game. I am still learning and apologize for any blatant issues with my code.

This is a 3D game with animations from Blender.

I have a GameViewController and a GameController. The game has been working so far, but I am not able to get the transition without the error message.

When I try to transition to a new scene (prompted by a “gameOver” logic), the new scene shows up, but I get this error: Error: Scene <SCNScene:> is modified within a rendering callback of another scene (<SCNScene: >). This is not allowed and may lead to crash.

Here is my code – I’ve edited it to show only the relevant parts but please let me know if you need more information.

I don’t know if I need to be creating a new GameOverViewController and / or GameOverController somewhere, to instantiate a new renderer function for the new scene?
Please let me know if you have any suggestions, I appreciate your help!


//  GameViewController.swift

import UIKit
import SceneKit
import SpriteKit


protocol GameControllerDelegate: AnyObject {
    func decreaseScore()
    func gameOver2()
}

var score = 0


class GameViewController: UIViewController, SCNPhysicsContactDelegate, GameControllerDelegate {

    var gameView: SCNView {
        return self.view as! SCNView
    }

    
    var gameController: GameController!
  
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.gameController = GameController(sceneRenderer: gameView)
        self.gameController.gameDelegate = self
        gameView.scene!.physicsWorld.contactDelegate = self
      
    }
    
    
    func gameOver2() {
        gameScene?.isPaused = true
        let transition = SKTransition.doorsOpenVertical(withDuration: 1.0)
        gameView.present(gameOverScene!, with: transition, incomingPointOfView: nil, completionHandler: {
            print("completionhandler")
        })
        
    }

        
}





//  GameController.swift


import SceneKit
import UIKit
import SpriteKit



var gameScene = SCNScene(named: "Art.scnassets/Game.scn")
var endgameScene = SCNScene(named: "Art.scnassets/GameOver.scn")


class GameController: NSObject, SCNSceneRendererDelegate {


    var scene: SCNScene
    let sceneRenderer:   SCNSceneRenderer
    var gameViewController: GameViewController!
    var gameView: GameController!
    lazy var gamePlaying = true
    weak var gameDelegate: GameControllerDelegate?

    
    init(sceneRenderer renderer: SCNSceneRenderer) {
        sceneRenderer = renderer
        scene = gameScene!
        super.init()
        sceneRenderer.delegate = self
        sceneRenderer.scene = gameScene
        
    }
    

    func renderer(_ renderer: SCNSceneRenderer, didSimulatePhysicsAtTime time: TimeInterval) {

        camera?.physicsBody?.angularVelocityFactor = SCNVector3(0, 0, 0)
        camera?.position = SCNVector3(x: -7, y: ((pwet?.presentation.position.y)! + 4), z: 60)

        if gamePlaying == true {
            if score < 0 {
                DispatchQueue.main.async {
                    self.gamePlaying = false
                    self.gameDelegate?.gameOver2()
                }
            }
    }
    
   
}

New contributor

Tiger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật