I just created a EventList as stated in the docs, but wondered if it’s possible to define them with a parameter. In the screenshot the UnityEvent is created, but there is no paramter (e.g. string, int, etc…). Wild guess is, this may be me again, not knowing TypeScript too well
Manually would be by excluding it from being generated by adding @nonSerialized (you can find info about annotations in the docs about the component compiler)
What’s the proper syntax to then subscribe another component’s method to that event?
I’ve added the following to a ComponentA //@type UnityEngine.Events.UnityEvent<string> @serializable(EventList) onStuffChange: EventList;
Then I’m firing the event at some point with this.onStuffChange?.invoke("stuff");
In another ComponentB, I have a reference to a ComponentA and a method I want to subscribe
checkChange(change: string): void {
console.log(change);
}How do I do that? Trying this.componentA.onStuffChange.addEventListener(checkChange);` gives me an error
“Cannot read properties of undefined”
Ok, for some reason, the EventList, although serialized, was undefined.
I guess empty EventList just get stripped?
Anyway, forcing initialization fixed it. //@type UnityEngine.Events.UnityEvent<string> @serializable(EventList) public onStuffChange: EventList = new EventList;
myStr: string;
start(): void {
console.log(this.myStr); //--> prints the value I have set in Unity's inspector
this.otherComponent.onStuffChange.addEventListener(this.checkChange);
}
checkChange(change: string): void {
console.log(change); //--> prints the value I'm getting from the event OK
console.log(this.condition); //--> prints undefined ??
}```
*by user 732122195614105653*