Hi, I´m trying to network each player color based on the spheres network example. So each of my player´s controller (which already have a reference to its SyncedTransform and a PlayerState) have a reference to a Renderer which is a Skinned Mesh Renderer. In local all works fine but when other players spawn, their material don´t show in my local player.
This is the code for the material, hope that youre doing fine and you could help me
@serializable(Renderer)
mainBodyRenderer?:Renderer;
...
start(): void {
...
const isLocal = this.isLocalPlayer();
if(isLocal){
//If the instance of the object is Local then -> Request all of that instance
this.gameObject.getComponent(SyncedTransform)?.requestOwnership();
this.animator?.setBool("move",true);
try {
let index = this.userProfileInfo!.Avatar_color as number;
const mat = this.mainBodyRenderer!.sharedMaterial as MeshStandardMaterial;
if(mat){
const coloredMat = new MeshStandardMaterial();
coloredMat.copy(mat);
coloredMat.color = this._avatarSettings!.avatarColors![index];
this.mainBodyRenderer!.sharedMaterial = coloredMat;
}
} catch (error) {
console.log("No user info found");
}
}
...
isLocalPlayer(): boolean{
const isConnected = this.context.connection.isInRoom;
return !this.playerState || !isConnected || this.playerState.isLocalPlayer;
}
Also mind that you should hook the initialization of your player to when the owner is set to the PlayerState component.
There’s no guarantee that the PlayerState got the remote owner set, so by the time your start is fired in your player component, the owner can be undefined. So to implement this properly, utilize the callback as it is shown in the sample
Oh Thank you : ), I will try . Right now (without implement the this.playerState.onFirstOwnerChangeEvent.addEventListener(() => this.initialize());) when my players spawn, both of them see all with their local color .
I will implement the last comment. Thanks
I have check it and is correct, each local player has its own data. One question, what does the this.gameObject.getComponent(PlayerState)?.onFirstOwnerChangeEvent.addEventListener(() => this.somefunction()) do ?