Search Unity

Third Party Syncing Color Materials? [PUN]

Discussion in 'Multiplayer' started by Zathos91, Nov 17, 2014.

  1. Zathos91

    Zathos91

    Joined:
    Aug 26, 2014
    Posts:
    45
    Hi guys, always working with my multiplayer game.

    I created with the 4.6 UI a system that let you choose the armor color that you like. The fact is now that i dont know how to sync every player color over the network.

    I was tryng to work with RPC, by creating a function, in my NetworkCharacter script that is "followed" by PhotonView component on the player prefab :


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. //we use photon.mono because there is a built-in photonview object
    5.  
    6. public class NetworkCharacter : Photon.MonoBehaviour {
    7.  
    8.     Vector3 realPosition = Vector3.zero;
    9.     Quaternion realRotation = Quaternion.identity;
    10.     Vector3 velocity = Vector3.zero;
    11.  
    12.     Color currentColor;
    13.     GameObject armor; //that's the gameObject child of PlayerPrefab that has the color changed
    14.     Animator anim;
    15.  
    16.     // Use this for initialization
    17.     void Awake () {
    18.         armor = transform.Find ("Armatura").gameObject; //Finds it on the hierarchy
    19.         currentColor = armor.renderer.material.color;
    20.         anim = GetComponent<Animator> ();
    21.     }
    22.  
    23.     void Start(){
    24.         photonView.RPC("SetColor",PhotonTargets.AllBuffered,null); //Set color
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void Update () {
    29.         if (photonView.isMine)
    30.         {
    31.             //do nothing
    32.  
    33.         }
    34.         else
    35.         {
    36.             //from where we are to actually where we should be
    37.             transform.position = Vector3.Lerp(transform.position,realPosition,0.1f)+ velocity*Time.deltaTime;
    38.             transform.rotation = Quaternion.Lerp(transform.rotation,realRotation,10*Time.deltaTime);
    39.  
    40.         }
    41.     }
    42.  
    43.     void OnPhotonSerializeView(PhotonStream stream,PhotonMessageInfo info){
    44.         if (stream.isWriting) {
    45.             //this is OUR player. We need to send our actual position to the network
    46.             stream.SendNext(transform.position);
    47.             stream.SendNext(transform.rotation);
    48.             stream.SendNext(rigidbody.velocity);
    49.             stream.SendNext(anim.GetBool("IsWalking"));
    50.             stream.SendNext(anim.GetBool("IsRunning"));
    51.         } else {
    52.             //this is someone else's player. WE Need to recieve the position and update
    53.             //our versione of he player
    54.             realPosition = (Vector3)stream.ReceiveNext();
    55.             realRotation = (Quaternion)stream.ReceiveNext();
    56.             velocity = (Vector3)stream.ReceiveNext();
    57.             anim.SetBool("IsWalking", (bool)stream.ReceiveNext());
    58.             anim.SetBool("IsRunning",(bool)stream.ReceiveNext());
    59.         }
    60.     }
    61.  
    62.     [RPC]
    63.     public void SetColor(){
    64.         if(photonView.isMine)
    65.             armor.renderer.materials[0].color = currentColor;
    66.     }
    67. }
    68.  
    At this moment, the color is the same for every client. Every istance of the game has the color that they choose initially....
    Any suggestions?

    Thanks
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,070
    I guess the problem is that you only apply color changes to those objects that are yours.
    In SetColor() you check photonView.isMine. That is only true for the player who is changing the color.
     
    Zathos91 likes this.
  3. Zathos91

    Zathos91

    Joined:
    Aug 26, 2014
    Posts:
    45
    thanks for the answer but still doesnt work....really i'm struggling with this all this day....

    (sorry for posting in exitgames the same 3d)
     
  4. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    You dont send any color info....
     
    Zathos91 likes this.
  5. Zathos91

    Zathos91

    Joined:
    Aug 26, 2014
    Posts:
    45
    can you please be more precise? I'm kinda new of networking
     
  6. Zathos91

    Zathos91

    Joined:
    Aug 26, 2014
    Posts:
    45
    SOLVED. thanks anyway
     
  7. janeshvarmishra

    janeshvarmishra

    Joined:
    Aug 24, 2015
    Posts:
    5
    hello how you have solved this problem can you answer please i have same type of problem.
     
  8. chrisabranch

    chrisabranch

    Joined:
    Aug 15, 2014
    Posts:
    146
    i would like to know how it was solved.
    i have the same problem
     
  9. flixtgi

    flixtgi

    Joined:
    Sep 17, 2017
    Posts:
    6
    How did you solve this?
     
  10. malthew

    malthew

    Joined:
    Feb 4, 2021
    Posts:
    1
    How did you solve it mate?
     
  11. macer2000

    macer2000

    Joined:
    Dec 22, 2019
    Posts:
    1
    I tried doing this(the below script) it worked for me.

    private void Start()
    {
    Vector3 color = new Vector3(Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255));
    view = GetComponent<PhotonView>();
    if (view.IsMine)
    {
    view.RPC("setColor", RpcTarget.AllBuffered, color);
    }
    }

    [PunRPC]
    public void setColor(Vector3 randomColor)
    {
    Color bColor = new Color(randomColor.x/255f, randomColor.y/255f, randomColor.z/255f);
    gameObject.GetComponent<Renderer>().material.color = bColor;
    }

    Hope this helps.
     
    Goebbelens and alexblackcal like this.