Trying to networking players materials :

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 :slightly_smiling_face:

 @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; 
    }

image.png

Original Post on Discord

by user 632418299711324161

The code that sets the material is conditioned by isLocal, you want to set it regardless of whether the player it is local.

Oh I see, let me try. Thanks

by user 632418299711324161

:slightly_smiling_face:

by user 632418299711324161

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 :slight_smile:

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

by user 632418299711324161

It doesn´t work :frowning_with_open_mouth: players see all other players with its local color.

by user 632418299711324161

That will be most probably an issue with the driving value you use to set the player color.
image.png

Is it relative to client or absolute on all instances?
As in, if the local player effects that value in any way etc.

Is using the localStorage this.userProfileInfo = JSON.parse(localStorage.user_profile);, it should be relative to each client

by user 632418299711324161

Have you logged what you get from the local storage? Check the data you run through your code if they are correct :slight_smile:

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 ?

by user 632418299711324161

It solves an edge case when the owner of PlayerState is undefined.

So if its a remote instance then the owner of PlayerState is undefined and calls the somefunction of that remote instance ?

by user 632418299711324161

No, this makes sure that the the logic is run only when the owner is there otherwise it registers to a callback on when it is received.

So do i understand that you are using a local stored value on your remote clients?

Yea.

by user 632418299711324161

That would be the bug, right?

So then you need to synchronize the choice for every user.

Well I´m trying sending and receiving messages but i´m a bit stuck. One question, there is a way to network the material using @syncfield ?

by user 632418299711324161