Help an idiot - Simple button!

Hi. I’m trying to set up a voting system, I believe everything is set up correctly but the variables seem to have trouble actually synchronizing. My code is as follows.

Variables:

        public int button1vote;
	public int button2vote;
	public int button3vote; 

	public int BTN1;
	public int BTN2;
	public int BTN3;

Button:

		if (GUI.Button (new Rect (140, 70, 350, 50), quiz.Answer1))
		{
		

			button1vote += 1;
			
			}

Serialization

	void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
	{
	
		if (stream.isWriting)
			
		{
			
			BTN1 = button1vote;
			stream.Serialize(ref BTN1);
			
		} else {
			
			stream.Serialize(ref BTN1);
			button1vote = BTN1;
	
			

		}

	}

What’s the problem? What could the reason for these variables not syncing? Am I missing something completely obvious?

Hello,
Make sure that the “observed” field in the object’s network view is this script. For what you’re doing, it may be easier to use RPCs.

This.

RPC’s would just be easier for this situation, you could just simply do:

public int button1votes;


void OnGUI(){

        if (GUI.Button (new Rect (140, 70, 350, 50), quiz.Answer1))

          {

             networkView.RPC("UpdateVotes", RPCMode.All,  "button1");

            }

}

[RPC]
void UpdateVotes(string whichButton){
if (whichButton == "button1"){
button1votes +=1;
    }
}

The script is attached to a game object with the objects transform set as the network view. I’m assuming this is incorrect then?

The difficulty with your current approach is ownership - who owns the GameObject with this script attached? Isn’t that player then the only one who can vote? Or do you have one GameObject per player, in which case you don’t need ints for the vote counters. So you could do it that way, just synchronizing an enum instead (the player’s current vote), and have the host gather the results and broadcast it out.

But it is much easier to do it using RPCs.

The game is largely text based with a voting system. Each player has the game object which allows them to vote. I’ve added an int counter so that the votes can be tallied up and calculated. I wouldn’t really know where to start with Enums, the only time I have used Enums is when I have been dealing with GUI states.

You want your own script in the network view. In the inspector, drag your script component into the observed field to override the transform.

1477092--81556--$class-NetworkView-0.jpg
Instead of “GameObject(Transform)” , you want your “GameObject(YourScript)”

More info: http://docs.unity3d.com/Documentation/Components/class-NetworkView.html