Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' 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.  
    6. namespace FyMa2618.UMT.NetworkVariables
    7. {
    8.     public class ColourPicker : MonoBehaviour
    9.     {
    10.         public void SelectColor(int colorIndex)
    11.         {
    12.             print(colorIndex);
    13.             ulong localClientId = NetworkManager.Singleton.LocalClientId;
    14.  
    15.             if(!NetworkManager.Singleton.ConnectedClients.TryGetValue(localClientId, out NetworkClient networkClient))
    16.             {
    17.                 return;
    18.             }
    19.             if(!networkClient.PlayerObject.TryGetComponent<PlayerColor>(out PlayerColor playerColor))
    20.             {
    21.                 return;
    22.             }
    23.  
    24.             playerColor.SetColorServerRpc((byte)colorIndex);
    25.         }
    26.     }
    27. }
    28.  
    29.  
    30.  
    31.  
    32.  
    33. //PlayerColor(Is on the Player prefab)
    34. using MLAPI;
    35. using MLAPI.Messaging;
    36. using MLAPI.NetworkVariable;
    37. using UnityEngine;
    38.  
    39. namespace FyMa2618.UMT.NetworkVariables
    40. {
    41.     public class PlayerColor : NetworkBehaviour
    42.     {
    43.  
    44.         public Renderer ColourRenderer;
    45.         public Material player;
    46.         public Color[] Colours;
    47.  
    48.         private NetworkVariableByte colorIndex = new NetworkVariableByte();
    49.  
    50.         [ServerRpc]
    51.         public void SetColorServerRpc(byte newColorIndex)
    52.         {
    53.             if(newColorIndex > 3) { return; }
    54.  
    55.             colorIndex.Value = newColorIndex;
    56.         }
    57.  
    58.         private void OnEnable()
    59.         {
    60.             print("Enable");
    61.             colorIndex.OnValueChanged += OnColorChanged;
    62.         }
    63.  
    64.         private void OnDisable()
    65.         {
    66.             print("Disbale");
    67.             colorIndex.OnValueChanged -= OnColorChanged;
    68.         }
    69.  
    70.         private void OnColorChanged(byte oldColorIndex, byte newColorIndex)
    71.         {
    72.             if(!IsClient) { return; }
    73.  
    74.             print("Suces");
    75.             //ColourRenderer.material.SetColor("_BaseColor", Colours[newColorIndex]);
    76.             player.color = Colours[newColorIndex];
    77.             print("AASuces");
    78.         }
    79.     }
    80. }
    81.  
     
  2. unity_FUc_6LWWe7c3mw

    unity_FUc_6LWWe7c3mw

    Joined:
    Jan 1, 2021
    Posts:
    43
    Sory that there is a emojy its because i maked ) ;
     
  3. Qazuro

    Qazuro

    Joined:
    Aug 4, 2020
    Posts:
    9
    This guy has a tutorial where he changes color of players using MLAPI and networked variables

    It seems to be a clean way of doing what you're doing if you still need it