Search Unity

Help Wanted Change material with RPC function

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

  1. Palmtendo

    Palmtendo

    Joined:
    Dec 22, 2020
    Posts:
    87
    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;
    6.  
    7. public class CharacterCustomization : MonoBehaviourPunCallbacks
    8. {
    9.     [SerializeField] Color[] allColors;
    10.  
    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");
    4.  
    5.         if (!myPV.IsMine)
    6.             return;
    7.  
    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

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,714
    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.
unityunity