Syncing Color Materials? [PUN]

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 :

using UnityEngine;
using System.Collections;

//we use photon.mono because there is a built-in photonview object

public class NetworkCharacter : Photon.MonoBehaviour {

    Vector3 realPosition = Vector3.zero;
    Quaternion realRotation = Quaternion.identity;
    Vector3 velocity = Vector3.zero;

    Color currentColor;
    GameObject armor; //that's the gameObject child of PlayerPrefab that has the color changed 
    Animator anim;

    // Use this for initialization
    void Awake () {
        armor = transform.Find ("Armatura").gameObject; //Finds it on the hierarchy
        currentColor = armor.renderer.material.color;
        anim = GetComponent<Animator> ();
    }

    void Start(){
        photonView.RPC("SetColor",PhotonTargets.AllBuffered,null); //Set color
    }
   
    // Update is called once per frame
    void Update () {
        if (photonView.isMine)
        {
            //do nothing

        }
        else
        {
            //from where we are to actually where we should be
            transform.position = Vector3.Lerp(transform.position,realPosition,0.1f)+ velocity*Time.deltaTime;
            transform.rotation = Quaternion.Lerp(transform.rotation,realRotation,10*Time.deltaTime);

        }
    }

    void OnPhotonSerializeView(PhotonStream stream,PhotonMessageInfo info){
        if (stream.isWriting) {
            //this is OUR player. We need to send our actual position to the network
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
            stream.SendNext(rigidbody.velocity);
            stream.SendNext(anim.GetBool("IsWalking"));
            stream.SendNext(anim.GetBool("IsRunning"));
        } else {
            //this is someone else's player. WE Need to recieve the position and update
            //our versione of he player
            realPosition = (Vector3)stream.ReceiveNext();
            realRotation = (Quaternion)stream.ReceiveNext();
            velocity = (Vector3)stream.ReceiveNext();
            anim.SetBool("IsWalking", (bool)stream.ReceiveNext());
            anim.SetBool("IsRunning",(bool)stream.ReceiveNext());
        }
    }

    [RPC]
    public void SetColor(){
        if(photonView.isMine)
            armor.renderer.materials[0].color = currentColor;
    }
}

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

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.

1 Like

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)

You dont send any color info…

1 Like

can you please be more precise? I’m kinda new of networking

SOLVED. thanks anyway

hello how you have solved this problem can you answer please i have same type of problem.

i would like to know how it was solved.
i have the same problem

How did you solve this?

How did you solve it mate?

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();
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().material.color = bColor;
}

Hope this helps.

2 Likes