Search Unity

How to change a Material for different clients whit MLAPI?!

Discussion in 'Netcode for GameObjects' started by unity_FUc_6LWWe7c3mw, Aug 17, 2021.

  1. unity_FUc_6LWWe7c3mw

    unity_FUc_6LWWe7c3mw

    Joined:
    Jan 1, 2021
    Posts:
    43
    Good day,
    i try to make a new multiplayergame with MLAPI i started this day so i ont have much experiecense btw. I passed all what i need but yet i dont pass the goal, i have a playerspawner-Script they are colored with one btw. the same Material(I spawn the Prefab). But rigth now i want to make a Color choser the Problem is when i change my Material all characters change i first tryed the Color chooser whith a video, in the Video the Person chnaged it with the renderer of the object / prefab (ColourRenderer.material.SetColor("_BaseColor", Colours[newColorIndex]);) after i searched on the Web and in the Documentations usw. but i found nothing to this theme and so i am here. (I have images where i give the numbers this work perfectly i tested it whith a print method, and than i have a array of colors that i put in the PlayerColor script bzw. when i get index 1 color Array one is the Actual Color)
    Thanks to advance to all!
    *Details*
    Unity version: 2021.1.17f1,
    MLAPI version: 0.1.0
    (The Color change code is down bellow)

    Code (CSharp):
    1. //ColourPicker (Is in a emty object where all canvasess are in!)
    2. using MLAPI;
    3. using MLAPI.Connection;
    4. using UnityEngine;
    5. namespace FyMa2618.UMT.NetworkVariables
    6. {
    7.     public class ColourPicker : MonoBehaviour
    8.     {
    9.         public void SelectColor(int colorIndex)
    10.         {
    11.             print(colorIndex);
    12.             ulong localClientId = NetworkManager.Singleton.LocalClientId;
    13.             if(!NetworkManager.Singleton.ConnectedClients.TryGetValue(localClientId, out NetworkClient networkClient))
    14.             {
    15.                 return;
    16.             }
    17.             if(!networkClient.PlayerObject.TryGetComponent<PlayerColor>(out PlayerColor playerColor))
    18.             {
    19.                 return;
    20.             }
    21.             playerColor.SetColorServerRpc((byte)colorIndex);
    22.         }
    23.     }
    24. }
    25. //PlayerColor(Is on the Player prefab)
    26. using MLAPI;
    27. using MLAPI.Messaging;
    28. using MLAPI.NetworkVariable;
    29. using UnityEngine;
    30. namespace FyMa2618.UMT.NetworkVariables
    31. {
    32.     public class PlayerColor : NetworkBehaviour
    33.     {
    34.         public Renderer ColourRenderer;
    35.         public Material player;
    36.         public Color[] Colours;
    37.         private NetworkVariableByte colorIndex = new NetworkVariableByte();
    38.         [ServerRpc]
    39.         public void SetColorServerRpc(byte newColorIndex)
    40.         {
    41.             if(newColorIndex > 3) { return; }
    42.             colorIndex.Value = newColorIndex;
    43.         }
    44.         private void OnEnable()
    45.         {
    46.             print("Enable");
    47.             colorIndex.OnValueChanged += OnColorChanged;
    48.         }
    49.         private void OnDisable()
    50.         {
    51.             print("Disbale");
    52.             colorIndex.OnValueChanged -= OnColorChanged;
    53.         }
    54.         private void OnColorChanged(byte oldColorIndex, byte newColorIndex)
    55.         {
    56.             if(!IsClient) { return; }
    57.             print("Suces");
    58.             //ColourRenderer.material.SetColor("_BaseColor", Colours[newColorIndex]);
    59.             player.color = Colours[newColorIndex];
    60.             print("AASuces");
    61.         }
    62.     }
    63. }
    64.  

    Sory that there is a emojy its because i maked ) ;
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Have you seen this?
     
    FlightOfOne likes this.