Adding a child AR model on toggle

Hi,

I have an app which adds models to a scene depending on the button pressed from a HStack, which works well. I’d like to add a child (which is just a box) to the model scene if a toggle is then true, and remove it if it then becomes false. Is there any way to do this?

In my code, the addCuboid bool changes fine but the if statement can’t see the modelEntity. Any help is much appreciated.

struct ARViewContainer: UIViewRepresentable {
@Binding var confirmedModel: String?
@Binding var addCuboid: Bool
@Binding var cuboidWidth: Float
@Binding var cuboidHeight: Float
@Binding var cuboidDepth: Float

func makeUIView(context: Context) -> ARView {
    
    let arView = CustomARView(frame: .zero)
    
    
    
    return arView
    
}

func updateUIView(_ uiView: ARView, context: Context) {
    
    
    if let modelFilename = self.confirmedModel {
            
            print("DEBUG: adding model to scene - \(modelFilename)")
            
        let modelEntity = try! ModelEntity.loadModel(named: modelFilename)
            
        let anchorEntity = AnchorEntity(plane: .horizontal)
            anchorEntity.addChild(modelEntity)
            
        uiView.scene.addAnchor(anchorEntity)
        
        modelEntity.generateCollisionShapes(recursive: true)
        
        uiView.installGestures(.init(arrayLiteral: [.translation, .rotation]), for: modelEntity)
    
        
            DispatchQueue.main.async {
            self.confirmedModel = nil
            }
        
        }
    
    // Protection zone adding
    print(addCuboid)
    
    if addCuboid == true {
        let cuboid = MeshResource.generateBox(width: self.cuboidWidth, height: self.cuboidHeight, depth: self.cuboidDepth, cornerRadius: 30)
    
    let material = SimpleMaterial(color:UIColor(.blue.opacity(0.5)), isMetallic: false)
        
        
    let cuboidEntity = ModelEntity(mesh: cuboid, materials: [material])
        
        // This is a fudge change. Check 3D models.
        cuboidEntity.position.y += 0.1
    
        modelEntity.addChild(cuboidEntity)
        
    }
    
}