GameObject.instantiate Canvas bug

Instantiating with GameObject.instantiate or GameObject.instantiateSynced works in situation’s where its full of all sorts of objects until a canvas with canvas type components such as text or images are in it.

With out Canvas with text or image:


Error when there is a canvas with text/images:


If there is just a Canvas with no Text or Image, it will work.

Code example:

    @serializable(GameObject)
    itemToInstantiate?: GameObject;

    currentX: number = 0;
    
    start() {
        if (this.itemToInstantiate) {
            this.items.forEach((item, index) => this.spawnitemToInstantiate(item, index));
        }
    }

    private spawnitemToInstantiate(item: any, index: number) {
        if (this.itemToInstantiate) {
            const instance = GameObject.instantiate(this.itemToInstantiate);
            if (instance) {
                 this.currentX +=3 
                instance.translateX(this.currentX);
            }
        }
    }

Hi! It looks like you’re encountering issues with GameObject.instantiate or GameObject.instantiateSynced when instantiating a canvas containing text or images in Needle Engine. Based on the information you provided and the documentation, here’s a brief breakdown of potential causes and solutions:

Issues and Potential Solutions

  1. Error Context: The errors you’re receiving when the canvas contains text or images may be related to rendering or instantiation specifics in the engine. Check for properties or mandatory components not being set correctly when instantiating UI elements inside a canvas.

  2. Instancing Limitations: If you’re trying to instantiate objects with complex components (like UI elements with child components) without proper configuration, the instantiation may fail. Ensure that all required properties for UI elements are correctly defined in the instantiated prefabs.

  3. Render Settings: Make sure that the Canvas component has valid render settings. Properties such as renderOnTop, depthWrite, doubleSided, castShadows, and receiveShadows should be set according to your requirements (see the documentation).

Suggested Code

You might want to adjust your spawnitemToInstantiate method to include debug logging that captures significant errors or outputs during the instantiation process. Here’s a revised version of that method:

private spawnitemToInstantiate(item: any, index: number) {
    if (this.itemToInstantiate) {
        try {
            const instance = GameObject.instantiateSynced(this.itemToInstantiate, { components: true });
            if (instance) {
                this.currentX += 3;
                instance.translateX(this.currentX);
                console.log("Spawned instance:", instance);
            }
        } catch (error) {
            console.error("Instantiation error:", error);
        }
    }
}

Follow-Up Questions

  • Could you provide the exact error messages you’re encountering?
  • Are you using the latest version of Needle Engine? Specific updates may address issues related to UI instantiation and rendering.
  • Have you confirmed whether the texts/images in the canvas are correctly set up to work with the instantiated context?

Feel free to ask if you need more detailed assistance with any specific aspect of your setup!

1 replies left - Get a PRO license for more AI replies.
thread_MMnQAb8nHktU2D0JG7Fr4UUx
Reply to this message to continue the conversation

@marwie1
I managed to bypass the bug by instantiating all the objects under the Canvas.
So jam packed all objects under it.
Is this just a standard practice and not a bug?

Hi @Ariel

Could you send a bugreport if you haven’t already? I’m not sure if I understand the setup correctly. The canvas that you instantiate contains 3D objects (non canvas objects) too?

Check out the images with some example hirearchies.

Dosent work no matter what is in the Canvas - just text, or just an image and a combo.
Though a empty Canvas with no text or image, does work.

So the main goal was to instantiate a non-Canvas object, an empty, with a Canvas inside with standard text.

Ok, thanks, I can reproduce this here

Should be fixed in the next update. I think this issue never occured before because in most cases UI is loaded from a prefab or separate scene and was (so far) never cloned at runtime when it was already in the scene. Thanks for reporting! That’s a good issue to fix

Unity:

Web:

1 Like