Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Changing MATERIAL not working, player is pink?

Discussion in 'Multiplayer' started by Athalansy, Aug 29, 2015.

  1. Athalansy

    Athalansy

    Joined:
    Oct 7, 2014
    Posts:
    17
    Okay, I asked a while ago how to update player names, and now I'm trying to make a skin system, but for some reason changing the material doesn't work, it doesn't assign a material to mesh renderer, same thing when I try to change the texture, it doesn't get assigned at all..
    I have no idea why it's not working, as it shouldn't be much different from changing the name, here's the code:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. using UnityEngine.UI;
    5. public class Player_NameMaterial : NetworkBehaviour {
    6.     public string playerName;
    7.     public Material[] skins;
    8.     public Material playerSkin;
    9.  
    10.     void Start(){
    11.         playerName = PlayerPrefs.GetString("Name");
    12.         playerSkin = skins[PlayerPrefs.GetInt ("ActiveSkin")];
    13.         CmdSendNameToServer(playerName);
    14.         CmdSendSkinToServer(playerSkin);
    15.     }
    16.  
    17.     [Command]
    18.     void CmdSendNameToServer(string nameToSend)
    19.     {
    20.         RpcSetPlayerName(nameToSend);
    21.     }
    22.     [Command]
    23.     void CmdSendSkinToServer(Material skinToSend){
    24.         RpcSetPlayerSkin(skinToSend);
    25.     }
    26.    
    27.     [ClientRpc]
    28.     void RpcSetPlayerName(string name)
    29.     {
    30.         gameObject.transform.FindChild("Canvas").transform.FindChild ("Text").GetComponent<Text>().text = name;
    31.     }
    32.     [ClientRpc]
    33.     void RpcSetPlayerSkin(Material skin)
    34.     {
    35.         gameObject.GetComponent<MeshRenderer>().material = skin;
    36.     }
    37.  
    38. }
    39.  
     
  2. Leoo

    Leoo

    Joined:
    May 13, 2013
    Posts:
    96
    Why are you sending the "Material Skin" ?
    The server should already know the available skins, then the client tells the server what skin to choose from that list.
    Just send the ActiveSkin int, then apply that one from the skin list.

    Code (csharp):
    1.  
    2.  
    3. public Material[] skins;
    4. public int playerSkin;
    5.  
    6. void Start(){
    7. playerSkin = PlayerPrefs.GetInt("ActiveSkin");
    8. CmdSendSkinToServer(playerSkin);
    9. }
    10.  
    11. [Command]
    12. CmdSendSkinToServer(int playerSkinInt){
    13. RpcSetPlayerSkin(playerSkinInt);
    14. }
    15.  
    16. [ClientRpc]
    17. RpcSetPlayerSkin(int playerSkinInt){
    18. gameObject.GetComponent<Renderer>().material = skins[playerSkinInt];
    19. }
    20.  
    21.  
     
    Athalansy likes this.
  3. Athalansy

    Athalansy

    Joined:
    Oct 7, 2014
    Posts:
    17
    Okay, I'm retarded, have no idea how I didn't figure that one out..
    Thank you very much!