Buttons repeat

Do you guys also see buttons repeating ?

The Toggle rotation button has been added in a component.

    start() {
        this.context.menu.appendChild({
            label: 'Toggle Rotation',  
            icon: 'camera',  
            onClick: () => {
                this.toggleRotation();  
            }
        })

    }

Hello Rahul Bhatia,

It seems you are experiencing an issue where buttons, specifically the “Toggle Rotation” button, are repeating in your Needle Engine component.

The code you shared for adding the button looks correct, but repeated instances might be caused by start() being called multiple times. To address this, you could implement a check to see if the button is already present before adding it again.

Here’s how you might modify the code to prevent button duplication:

start() {
    // Check if the button already exists
    if (!this.context.menu.querySelector('[data-needle="toggle-rotation"]')) {
        this.context.menu.appendChild({
            label: 'Toggle Rotation',  
            icon: 'camera',  
            onClick: () => {
                this.toggleRotation();  
            },
            'data-needle': 'toggle-rotation' // Add a unique identifier to the button
        });
    }
}

This code ensures that before appending the button, it checks if a button with the same data attribute is already present, preventing the duplication.

If you continue to encounter this issue, please provide more context, such as whether the start() method is being called multiple times or if this is part of a larger component lifecycle.

For more information on handling menus and buttons, you might find this documentation helpful: Needle Engine Docs.

If you have further questions or need more specific guidance, feel free to ask!

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

Does the component exist multiple times in your scene? Same with SyncedRoom component: do they exist more than once? Or do you load e.g. sub-scenes that have them?

It exists exactly once. It is attached to the camera. There is only 1 scene. No scene switching.
There is only 1 synced room component. No sub-scenes.

I just created a new project in blender 4.4 and tried adding the custom button. Here it works correctly. i.e. the button shows up once, not twice.

Thanks for the reply. Will try to reproduce next week, I havent seen this behaviour before.

Youre using the latest engine version in unity here? Any other details that might be relevant?

Hello Rahul,

the issue does not reproduce on my side - could you provide more details about your scene / setup?

Which version of Needle Engine are you currently using? Does it reproduce via Needle-engine - Vite Template (forked) - StackBlitz ?