Hi everyone
Im trying to have characters on a network change skins depending on the players choice. my character prefab has a SkinnedMeshRenderer in its children. I want to access it and change the material on it. and i want other players on the network to also see the new skin on that players character, and not on theirs too. Skins are chosen in the main menu and saved as a integer to playerprefs. I have provided my class below, and I would appreciate any help with this. I hope this makes sense, as Im not a native english speaker. Thanks
using UnityEngine;
using System.Collections;
public class Instantiate : MonoBehaviour {
public Material skin1;
public Material skin2;
public Material skin3;
public Material skin4;
public Transform playerAvatar;
public NetworkView playerID;
public string playerName = "";
public int playerClass = 0;
public int playerSkin = 0;
public Transform PlayerCamera;
public SkinnedMeshRenderer skinner;
void Start () {
playerName = PlayerPrefs.GetString("AvatarName", "");
playerClass = PlayerPrefs.GetInt("AvatarClass", 0);
playerSkin = PlayerPrefs.GetInt("AvatarSkin", 0);
}
void OnNetworkLoadedLevel () {
// instantiate player when network is established
if (Network.isClient) {
Network.Instantiate(playerAvatar, transform.position, transform.rotation, 0);
playerID = GameObject.Find("Player(Clone)").networkView;
if(networkView.isMine) networkView.RPC("SetSkin", RPCMode.All, playerID, playerSkin);
}
}
void OnPlayerDisconnected (NetworkPlayer player) {
Network.RemoveRPCs(player, 0);
Network.DestroyPlayerObjects(player);
}
void OnDisconnectedFromServer () {
Destroy(GameObject.Find("Player(Clone)"));
Network.RemoveRPCsInGroup(0);
}
[RPC]
void SetSkin (NetworkView player, int skin) {
skinner = player.gameObject.GetComponentInChildren<SkinnedMeshRenderer>();
if (skin == 0) {
skinner.material = skin1;
}
if (skin == 1) {
skinner.material = skin2;
}
if (skin == 2) {
skinner.material = skin3;
}
if (skin == 3) {
skinner.material = skin4;
}
}
}