Why Instantiate AssetReference Button onClick Event is empty

i have 2 event in Button Prefab
image

Hi oot4chi! I’m an AI-powered assistant here to help you with questions about Needle Engine, three.js, and basic Unity or Blender help. Please reply with your question to receive further assistance.

For better support, could you provide your operating system, Needle Engine version, and Unity version? Thanks!

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

use unity 2022.3.21f1

Hi oot4chi! Since you’re using Unity 2022.3.21f1, if the onClick event for your Button prefab is empty, it could be due to a few reasons:

  1. Event Assignment: Ensure that the GameObject that contains the script with the method you want to call is properly referenced in the Button component. Assign the method to the onClick event in the Button inspector.

  2. Script Compilation or Errors: Check if there are any compilation errors in the console. If the script containing the method you want to call has errors, Unity will not recognize the method.

  3. Method Signature: Make sure that the method you want to call has the correct signature (no parameters and public access).

  4. Prefab Connection: If you’ve made changes to the prefab but not applied them, it might cause the onClick events to not reflect changes made to instances of the prefab.

Try these steps, and let me know if you need further help!

1 replies left - GPT 4O
thread_FDJEUamI2A2gYGWnLneIWW06
Reply to this message to continue the conversation

Can you please be a bit more detailed with your posts? It’s not clear what your setup and question is.

I created a Prefab that includes a script and a button inside it. I set up the OnClick event to call a function. When I place the Prefab directly in the scene, the OnClick event works correctly. However, when I instantiate the Prefab at runtime, the OnClick event does not work.

Can you show how you instantiate the prefab at runtime?
And can you log the instantiated Button and show what’s inside the OnClick EventList?

import { Behaviour, serializable, AssetReference} from "@needle-tools/engine";
import { Object3D } from "three";

export class TestScript extends Behaviour {
    @serializable(Object3D)
    parent?: Object3D;

    @serializable(AssetReference)
    button? : AssetReference;

    async start() {
       await this.button?.instantiate(this.parent);
    }
}

EventList Prefab in Scene

EventList Prefab instantiate at Runtime

Thank you. I think this is a known issue with how Events are serialized. They need to be rebinded to the new instance. I just added a new issue internally and will see if we can fix it in the next few updates

Hi, just a quick update: this issue will be fixed in the next engine version!

some issue with set image with instantiate

export class TestScript extends Behaviour {
    @serializable(Object3D)
    parent?: Object3D;

    @serializable(Texture)
    images? : Texture[];

    @serializable(AssetReference)
    button? : AssetReference;
    async start() {
        if(this.images){
            for(let i=0;i<this.images?.length;i++)
                {
                    const obj = await this.button?.instantiate(this.parent);
                    obj?.getComponent(CustomButtonComponent)?.setTexture(this.images[i]);
                }
            }
        }
    }
export class CustomButtonComponent extends Behaviour {

    @serializable(Image)
    img?: Image;

    setTexture(text? : Texture)
    {
         if(this.img && text){
            this.img.image = text;
            console.log(this.img.image.name);
        }
           
    }

first run this correctly.


Pressing refresh(f5) sometimes has problems.image incorrect.

I dont see “this.button” delcared in your example script. Can you reproduce this please using stackblitz? Needle-engine - Vite Template - StackBlitz


I found some issues, when I turn this off it works properly, no bugs with image set texture with instantiate.
Why?

Can you reproduce this with just the script that sets the texture in a new scene and report it as a bug?

Progressive Loading does at runtime load better quality textures for the ones that are exported in your scene and automatically generate mesh LOD levels. I’m not sure what exactly is causing the wrong assignment here but would like to understand it better. For that the bugreport would be useful. Please don’t send your whole project but only what is necessary to reproduce the bug.

The EventList issue has been fixed in the latest release

how about set image texture bug ?