title: SwiftUI – Passing data from SwiftUIView to SceneKit
body: <p>Goal: 
  SwiftUI toggle button that controls a SwiftUI View with Scenekit (UIViewRepresentable)</p>
  
  <p>// show statistics such as fps and timing information</p>
  
  <p>What I did:
  This is the UIViewRepresentable ScenekitView</p>
  
  <pre><code>import SwiftUI
  import SceneKit
  
  struct ScenekitView : UIViewRepresentable {
      let scene = SCNScene(named: "art.scnassets/ship.scn")!
  
      func makeUIView(context: Context) -&gt; SCNView {
          // create and add a camera to the scene
          let cameraNode = SCNNode()
          cameraNode.camera = SCNCamera()
          scene.rootNode.addChildNode(cameraNode)
  
          // place the camera
          cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
  
          // create and add a light to the scene
          let lightNode = SCNNode()
          lightNode.light = SCNLight()
          lightNode.light!.type = .omni
          lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
          scene.rootNode.addChildNode(lightNode)
  
          // create and add an ambient light to the scene
          let ambientLightNode = SCNNode()
          ambientLightNode.light = SCNLight()
          ambientLightNode.light!.type = .ambient
          ambientLightNode.light!.color = UIColor.darkGray
          scene.rootNode.addChildNode(ambientLightNode)
  
          // retrieve the ship node
          let ship = scene.rootNode.childNode(withName: "ship", recursively: true)!
  
          // animate the 3d object
          ship.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: 2, z: 0, duration: 1)))
  
          // retrieve the SCNView
          let scnView = SCNView()
          return scnView
      }
  
      func updateUIView(_ scnView: SCNView, context: Context) {
          scnView.scene = scene
  
          // allows the user to manipulate the camera
          scnView.allowsCameraControl = true
  
          // show statistics such as fps and timing information
  
          scnView.showsStatistics = true
  
          // configure the view
          scnView.backgroundColor = UIColor.black
  
   }
  
  }
  struct ScenekitView_Previews: PreviewProvider {
      static var previews: some View {
          ScenekitView()
      }
  }
  </code></pre>
  
  <p>This is the Menu to control Scenekit</p>
  
  <pre><code>import SwiftUI
  
  
  struct Menu: View {
  
   @State var showstats = false
  
  
      var body: some View {
  
          HStack {
  
          Form {
              Toggle(isOn:) {
                 Text("Stats")
              }
          }
          .frame(width: 200.0)
  
              Spacer()
      }
    }
   }
  struct Menu_Previews: PreviewProvider {
      static var previews: some View {
          Menu()
      }
  }
  </code></pre>
  
  <p>And this is Main Scene, with Menu on top of Scenekit:</p>
  
  <pre><code>import SwiftUI
  
  struct MainView: View {
      var body: some View {
  
          ZStack {
          ScenekitView()
          Menu()
          }
  
  
      }
  }
  </code></pre>
  
  <p>Question: How can I pass data from ScenekitView to Menu, so I can toggle and show/hide stats?</p>
  
  <p>I tried ObservableObject but could not make it work. Also looked into other SO threads, but none worked for me.</p>
  
  <p>Any help is much appreciated!</p>
  
  <p>Cheers to all<a href="https://i.stack.imgur.com/Kr0Tl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Kr0Tl.png" alt="enter image description here"></a></p>