I have a manager class which adds buttons at runtime to a set of quads and connects them to their content. In order to get all the necessary information at runtime, I’ve created a custom class which holds the element to which I am adding the button behaviour and the elements which it shows when clicked. This works well in a local scene, but I need it to work in a remote scene.
I am using your scene switcher component to load external scenes from URLs. Some of these scenes have the manager class described above. However, when the manager class is loaded as part of an external glb, the references to the objects which are part of the custom class are not correct. They have different uuids from the actual objects in the scene. So, when I add the button behaviour, it will not, in fact, be added to the correct Object3D which is in the scene, but to another object which does not exist in the hierarchy.
I’m attaching here a simplified example of this issue (no buttons, just console.log messages). This contains the custom class and the reference manager
import { Behaviour, GameObject, serializable } from "@needle-tools/engine";
import { Object3D } from "three";
// Documentation → https://docs.needle.tools/scripting
export class ReferenceContainer {
@serializable(Object3D)
transform: Object3D | undefined;
@serializable(GameObject)
gameobject: GameObject | undefined;
}
export class ReferenceTest extends Behaviour {
@serializable(ReferenceContainer)
container: ReferenceContainer[] = [];
@serializable(Object3D)
transformRef: Object3D | undefined;
@serializable(GameObject)
gameObjectRef: GameObject | undefined;
start() {
console.log("ReferenceTest: transformRef", this.transformRef);
console.log("ReferenceTest: gameObjectRef", this.gameObjectRef);
this.container.forEach((c, index) => {
console.log(`ReferenceTest: container[${index}] transform`, c.transform);
console.log(`ReferenceTest: container[${index}] gameobject`, c.gameobject);
});
}
}
Here is a SelfReference script to tell me what is in the scene.
import { Behaviour } from "@needle-tools/engine";
export class SelfReference extends Behaviour {
start() {
console.log("SelfReference: gameObject", this.gameObject);
}
}
This is the Unity scene
And here is a screenshot with what I get when I load the scene with:
const asset = AssetReference.getOrCreateFromUrl(url, this.context);
const instance = await asset.instantiate(this);
As you can see, the references from the containers are not actually part of the scene hierarchy, only the references to the objects directly are correct. So any component added to them will do nothing. I need to reference the objects in the scene.
How can I do that?