How do I change material at runtime?

Options
I am trying to change the material of my player at runtime in order to help distinguish players from eachother.
Currently, the changes go through, but they are applied to each player in the scene as opposed to the player that called the method.

public List RocketMaterials;
Renderer rocketRenderer;
private PhotonView _photonView;
private bool hasChangedMat=false;

void Start()
{
Debug.Log("Mat change start");
_photonView = GetComponent();
_photonView.RPC("ChangeRocketMaterial", RpcTarget.All);
}


[PunRPC]
public void ChangeRocketMaterial()
{
if (!hasChangedMat) {
Debug.Log("Change mat method;");
rocketRenderer = GetComponent();
rocketRenderer.enabled = true;

rocketRenderer.material = RocketColors[PhotonNetwork.PlayerList.Length];
hasChangedMat = true;
}
}

void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting == true)
{
stream.SendNext(hasChangedMat);
}
else
{
hasChangedMat = (bool)stream.ReceiveNext();
}
}

I am fairly certain that the problem is that the code does not define which players material should be uppdated, but I have no real idea how to do that with Photon.

I also tried using photonView.IsMine as a requirement to make sure only the LocalPlayer runs it, which technically solved the problem, but as far as I am aware right now, not in a usable way

Answers

  • Nori_SC
    Nori_SC
    edited November 2019
    Options
    that's all

    public bool On;
    void Update()
    {
    if (!On)
    {
    gameObject.GetComponent().material = null;
    gameObject.GetComponent().material = LightOff;
    }
    else
    {
    gameObject.GetComponent().material = null;
    gameObject.GetComponent().material = LightOn;
    }
    }
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
    if (stream.IsWriting)
    {
    stream.SendNext(On);
    }
    else if(stream.IsReading)
    {
    On = (bool)stream.ReceiveNext();
    }
    }