**OS : ** Windows 11
**Needle Engine: ** 3.51.0
**Unity: ** 2022.3.15
Hi everyone, I’m doing some calls to EventList and when I Invoke the EventList, the method which is listening in fact is executed but, a reference to a script inside that method is undefined and therefore the expected behavior is not executed. But is strange because that reference multiplayerController
in the start method is not undefined but, when is called in the method paralize
, that reference is undefined.
The next is my code :
import { Behaviour, GameObject, serializable } from "@needle-tools/engine";
import { MultiplayerController } from "./PlayerController";
import { LogicGameEvents } from "./TurretBehavior";
export class LogicEventPlayerHandler extends Behaviour{
@serializable(MultiplayerController)
multiplayerController !: MultiplayerController;
start(): void {
//This line returns that In fact there is a MultiplayerController which is not undefined
console.log(this.multiplayerController);
this.setBeginListener();
}
onDisable(): void {
this.setStopListeners();
}
setBeginListener(){
GameObject.findObjectOfType(LogicGameEvents)!.logicalEvent.addEventListener(this.paralize);
}
setStopListeners(){
GameObject.findObjectOfType(LogicGameEvents)!.logicalEvent.removeEventListener(this.paralize);
}
paralize(){
//When the paralize method is invoked for another class, for some reason the multiplayerController is undefined
console.log(this.multiplayerController);
this.multiplayerController?.paralize();
}
}
That event is declare in the class LogicalGameEvents
export class LogicGameEvents extends Behaviour{
//player_controls_event = new Event("player_controls");
@serializable(EventList)
logicalEvent !: EventList;
public invokeEvents(){
this.logicalEvent.invoke();
}
}```
And is invoked in `BulletBehavior` :
```ts
export class BulletBehavior extends Behaviour{
private logicalGameEvent !: LogicGameEvents;
start(): void {
this.logicalGameEvent = GameObject.findObjectOfType(LogicGameEvents)!;
const direction = this.gameObject.parent!.worldForward;
const gObj_rb = this.gameObject.getComponent(Rigidbody);
gObj_rb!.resetForcesAndTorques();
gObj_rb!.resetVelocities();
gObj_rb!.applyImpulse(direction.normalize().multiplyScalar(1.1));
this.timedDestroy();
}
onCollisionEnter(col: Collision) {
//const playerControls = col.gameObject.getComponent(MultiplayerController);
// if(playerControls){
// //this.dispatchEvent(LogicGameEvents.instance.player_controls_event);
// playerControls.paralize();
// }
this.logicalGameEvent.invokeEvents(); //< -- esto se esta invocando pegue o no pegue
GameObject.destroySynced(this.gameObject);
}
private lifeTime : number = 2;
timedDestroy = () => {
if(this.lifeTime === 0){
GameObject.destroySynced(this.gameObject);
}else{
this.lifeTime--;
setTimeout(this.timedDestroy, 1000);
}
}
}```
Why does the MultiplayerController reference is not null in the start of `LogicEventPlayerHandler` but, when the event `logicalEvent` is called in the method `paralize` of the same class, it comes out that the MultiplayerController reference (called multiplayerController ) is undefined ?