Looking for some help on a script [RPC] Networking
I have been learning C# and Unity on my own for a little while and am having trouble understanding why the following code isn't working properly.
public class PlayerTurn : MonoBehaviour {
GameObject gameManager;
PlayerDatabase dataScript;
public bool isMyTurn = false;
void Start () {
gameManager = GameObject.Find ("GameManager");
dataScript = gameManager.GetComponent<PlayerDatabase>();
if(networkView.isMine == true)
{
if( dataScript.PlayerList[0].networkPlayer == int.Parse (Network.player.ToString ()))
{
isMyTurn = true;
}
}
else
{
enabled = false;
}
}
void OnGUI()
{
if(isMyTurn == true)
{
if(GUI.Button (new Rect(50,50,130,50), "End Turn"))
{
isMyTurn = false;
for(int i = 0;i<dataScript.PlayerList.Count;i++)
{
if(dataScript.PlayerList[i].networkPlayer == int.Parse (Network.player.ToString ()))
{
networkView.RPC ("UpdateWhoIsPlayer", RPCMode.AllBuffered, i);
Debug.Log ("RPC from button, int: " + i);
}
}
}
}
}
[RPC]
void UpdateWhoIsPlayer(int player)
{
++player;
Debug.Log ("RPC Ran");
if (player > dataScript.PlayerList.Count)
{
player = 0;
Debug.Log ("Set player to 0");
}
if(dataScript.PlayerList[player].networkPlayer == int.Parse (Network.player.ToString ()))
{
isMyTurn = true;
Debug.Log ("isMyTurn is: " + isMyTurn);
}
}
}
The first player to join the game does get isMyTurn set to true and the button appears, when clicked you can see the debug commands show in the console. Here is the screen shot of the inspector/console after the RPC is recieved. http://i.imgur.com/A1cvfb7.png. It appears to set the bool true, but the GUI never shows up on the next player. Any tips are appreciated, I am very new to understanding RPC's and networking. Thanks.
Posted Jun 25, 2014, 6:03 AM UTC
You have two player objects with two separate copies of this script, I think. When you do an RPC from player 1, it updates player 1 correctly... But player 3 never recieves an RPC to know that it should be updated.
You could move the player turn id variable into a manager object and do the RPC there and then just have this script compare that ID to its own every frame.
Thanks for the help.