NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

Can SwiftUI environment objects be accessed within SpriteKit Scene?

Forums > SwiftUI

I tried accessing a SwiftUI environment object (that is available to regular SwiftUI views) from within Spritekit, but I am unable to access it. I always get an error indicating that the Environment Object is not available, but I have clearly loaded it before coming to the view containing the Spritekit scene. Can this even be accomplished?

class PlayerScene: SKScene { @EnvironmentObject var usersContainer: UsersContainer //container class with currently logged in users

override func didMove(to view: SKView) {
    for u in usersContainer.users{ //users is an array containing user objects
        let r = //does not work


I was able to make it work by changing it to an ObservedObject instead of an EnvironmentObject within SpriteKit


I am having the same problem. Can you give me example code that shows how you got it to work?


Yes, more code, please, @sri-dor. I've tried changing @Environment to @ObservedObject but without success. Clearly, I'm missing something. Thanks!


@caldantech...If you are still having the same problem, check out my own reply at Not sure if it is the exact answer you need, but maybe it will help. Good luck!


Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.