Instance member 'addChild' cannot be used on type 'AnchorEntity'

310 views Asked by At

I've been learning Swift and ARKit so please excuse my sloppy coding. I used the two tutorials below to create my existing code. I started with the first and then the error occurred at the very end of the second video. This didn't occur to the youtuber, so I'm not exactly sure where I went wrong. The full error reads "Instance member 'addChild' cannot be used on type 'AnchorEntity'; did you mean to use a value of this type instead?"

  1. https://www.youtube.com/watch?v=J7hRooEVBhU&list=WL&index=1)
  2. https://www.youtube.com/watch?v=8l3J9lwaecY)

Any help is greatly appreciated!

import UIKit

import RealityKit

import ARKit

class ViewController: UIViewController {

 @IBOutlet var arView: ARView!

 override func viewDidLoad() {
    super.viewDidLoad()
 
    arView.session.delegate = self
    
    showModel()
    overlayCoachingView()
    setupARView()
    
    arView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: 
#selector(handleTap(recognizer:))))
    
}

func showModel(){
    
    let anchorEntity = AnchorEntity(plane: .horizontal, minimumBounds:[0.2, 0.2])
    
    let entity = try! Entity.loadModel(named: "fender_stratocaster")
    entity.setParent(anchorEntity)
    
    arView.scene.addAnchor(anchorEntity)
    
}
func overlayCoachingView () {
    
    let coachingView = ARCoachingOverlayView(frame: CGRect(x: 0, y: 0, width: 
 arView.frame.width, height: arView.frame.height))
    
    coachingView.session = arView.session
    coachingView.activatesAutomatically = true
    coachingView.goal = .horizontalPlane
    
    view.addSubview(coachingView)
    
 }
    
    // Load the "Box" scene from the "Experience" Reality File
   // let boxAnchor = try! Experience.loadBox()
    
    // Add the box anchor to the scene
    //arView.scene.anchors.append(boxAnchor)

 func setupARView(){
    arView.automaticallyConfigureSession = false
    let configuration = ARWorldTrackingConfiguration()
    configuration.planeDetection = [.horizontal, .vertical]
    configuration.environmentTexturing = .automatic
    arView.session.run(configuration)
 }

 //object placement
 
@objc
func handleTap(recognizer: UITapGestureRecognizer){
    let location = recognizer.location(in:arView)
    
    let results = arView.raycast(from: location, allowing: .estimatedPlane, alignment: 
 .horizontal)
    
    if let firstResult = results.first {
        let anchor = ARAnchor(name: "fender_stratocaster", transform: 
firstResult.worldTransform)
        arView.session.add(anchor: anchor)
    } else {
        print("Object placement failed - couldn't find surface.")
    }
}

func placeObject(named entityName: String, for anchor: ARAnchor)  {
    let entity = try! ModelEntity.loadModel(named: entityName)
    
    entity.generateCollisionShapes(recursive: true)
    arView.installGestures([.rotation, .translation], for: entity)
    
    
    let anchorEntity = AnchorEntity(anchor: anchor)
    AnchorEntity.addChild(entity)
    arView.scene.addAnchor(anchorEntity)
    
    
    }
}
extension ViewController: ARSessionDelegate {
func session( session: ARSession, didAdd anchors: [ARAnchor]) {
for anchor in anchors {
    if let anchorName = anchor.name, anchorName == "fender_stratocaster" {
        placeObject(named: anchorName, for: anchor)
    }  }
}
}
1

There are 1 answers

0
Andy Jazz On BEST ANSWER

As I see, the issue comes from this line (and it looks like a misprint):

AnchorEntity.addChild(entity)        // type

However, you definitely need to use the following line instead:

anchorEntity.addChild(entity)        // instance of class

So your version can't be successfully run due to the fact AnchorEntity is a type, not a class instance.


About class object

When you create an instance you initialize a class (here, with its default properties: World(0,0,0)):

let anchorEntity = AnchorEntity()    // instantiating

If you do not intend to use instance, you might use a class itself:

AnchorEntity().addChild(entity)      // class