syncing colors

Hi :slight_smile:
i’m creating a network game
whenever i enter the client and press Connect it sends a request to the server and the server creates a gameobject with a random color.
the problem is that in the client the game object appears in different color and i need to sync the colors.
i tried to use rpc but i can’t pass gameobjects as parameters and passing the NetworkPlayer doesn’t help me.
How can i sync the color of the gameobjects?
Thx :slight_smile:

Server side:
if(randomcolor == Color.Red){
//send string to client “c=red” for example
}

Client side:
If(serverInfo == “c=red”)
GameObject.renderer.material.color = Color.Red

and if you’re using a random red/blue/green format then send 3 random integer values.
And then make the server side never call the random function again, so the color stays the same for everyone.

Thx for the response
but in the game there are multiple number of players and when i’ll write

ill have to specify which gameobject i want to access and since i can’t pass gameobjects as parameters in rpc i don’t have a gameobject to refer to in the client.

You could try sending the name of the gameobject and then use GameObject.Find(nameString).renderer.material.color = Color.Red;
But that probably wouldn’t work if there are gameobjects with the same name.
Maybe u could go into further detail about your project, so i am able to help better c:

  • Ivan

I’m building a cell(living cell) simulator for a project.
the simulator consists of 2 project, the server and the client

this is the main script in the client:

and these are the 2 main scripts in the server

(there’s also a script for handling the cell behavior but i guess it’s not important for the problem)
btw, i’m using this tutorial: 3dgep.com/?p=4609

thx for the help :slight_smile:

The owner of the object needs to update the clients which color the object has. This can be done using RPC’s.
An rpc gets called on the gameobject it is exececuted.
You can’t send a color through RPC so you will have to find another way. Convert it to int’s or serialize the color and send it.
So on the server you can do something like :

                Color CubeColor = Color.red;
                renderer.material.color = CubeColor;
                string xmlString = SerializeToXmlString(CubeColor);
                networkView.RPC("SetColor", RPCMode.AllBuffered, xmlString);

To serialize to an xml string:

    string SerializeToXmlString(Color thiscolor)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Color));
        StringWriter sw = new StringWriter();
        serializer.Serialize(sw, thiscolor);
        return sw.ToString();
    }

Then on the client just deserialize:

    [RPC]
    void SetColor(string playercolor)
    {
        Color c = DeserializeXmlString(playercolor);
        renderer.material.color = c;
    }

    Color DeserializeXmlString(string xmlString)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Color));
        StringReader sr = new StringReader(xmlString);
        return (Color)serializer.Deserialize(sr);
    }

that’s not the problem
i don’t have any problem passing the color as a parameter
my problem is to what gameobject i should assign the color in the client? i can’t pass gameobjects through rpc and all the gameobjects in my game have the same name so i can’t know…

You don’t have to know…
As I stated before, the command is executed on the GameObject it was called on.

Oh… so i can execute and recieve a rpc command from a script that’s attached to the game object?
Ok, it works
thank you for the help :slight_smile: