Search Unity

Help Wanted Change material with RPC function

Discussion in 'Multiplayer' started by Palmtendo, Jul 15, 2021.

  1. Palmtendo


    Dec 22, 2020
    I try to change the players material color when he clicks on a button. This works fine, but only happens on your computer and is not synced. How do I have to change the code so it syncs over the network?

    Here is the code that is attached to the canvas where all my color change buttons are located:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using System.IO;
    7. public class CharacterCustomization : MonoBehaviourPunCallbacks
    8. {
    9.     [SerializeField] Color[] allColors;
    11.     public void SetColor(int colorIndex)
    12.     {
    13.         PlayerManager.localPlayer.SetColor(allColors[colorIndex]);
    14.     }
    15. }
    The color index tells the player manager which color I choose and applies this color in the player manager script:
    Code (CSharp):
    1. public void SetColor(Color newColor)
    2.     {
    3.         //Debug.Log("Set Color");
    5.         if (!myPV.IsMine)
    6.             return;
    8.         myColor = newColor;
    9.         if (renderer != null)
    10.         {
    11.             renderer.material.color = myColor;
    12.         }
    13.     }
    myColor is just a static color.

    I guess I have to do something like this:
    Code (CSharp):
    1. .myPV.RPC("RPC_SetColor", RpcTarget.All);
    but I don't know where to place this in the code (myPV = Photonview attached to the player)
  2. tobiass


    Apr 7, 2009
    You need one method, which has the PunRPC attribute. This is the one you'd call with view.RPC("saidMethod", ...).
    Docs for RPCs.
    Color is not a type that PUN can de/serialize out of the box. Send it as a series of bytes or send an index value to a table of colors instead.
    Palmtendo likes this.