TypeError: Cannot read properties of undefined (reading 'transform')

Hi guys,
I have upgraded to Needle 3.47.0 from a far older version, 3.36 or there abouts. I am now getting an error accessing the transform of one of my objects and I dont know why. In the code below, I want to set the position of an object directly but it not longer works and gives me the error in the title of this post. The same occurs if I leave out the “.transform” and directly call “gameObject.position.set”.

What am I doing wrong here? Again, this has worked for months. The object isnt nul btw.

It is specifically this part giving the issue "
this.horseCameras[i].gameObject.transform.position.set

public focusOnRacerJS(index : number) {
  this.broadcastCameraActive = false;
  if (this.mainCamera != null && this.horseCameras != null) {
    GameObject.setActive(this.mainCamera.gameObject, false);

    //set the camera target to the racer
    for (let i = 0; i < this.horseCameras.length; i++) {
      if (i == index) {
        GameObject.setActive(this.horseCameras[i].gameObject, true);
        //reset the camera position
        this.horseCameras[i].gameObject.transform.position.set(this.horseCamDefaultPosition.x, this.horseCamDefaultPosition.y, this.horseCamDefaultPosition.z);

      } else {
        GameObject.setActive(this.horseCameras[i].gameObject, false);
      }
    }

}
}

Hi nostalgicbear! I’m an AI-powered assistant here to help with your questions. I can assist with Needle Engine, three.js, and basic Unity or Blender inquiries. Please reply with your question and don’t forget to include your OS, and Needle Engine, Unity or Blender versions if applicable! Looking forward to helping you!

2 replies left - GPT 4O
thread_Xlj8QzVexNux3TGYcPDka2KL
Reply to this message to continue the conversation

Hi @nostalgicbear

what do you see in the browser console when you log this.horseCameras[i].gameObject or this.horseCameras[i]?

Thanks Marcel, you led me to the solution as usual. It was undefined, due to me having declared the list as:

  @serializable(Object3D)
  public horseCameras?: Camera[];

instead of


  @serializable(Camera)
  public horseCameras?: Camera[];

Used to work in the older versions, so I completely overlooked that today.

This topic was automatically closed 13 days after the last reply. New replies are no longer allowed.