- Home /
How to synchronize GameObjects
Hello, First of all, sorry if I make any mistakes, it's not my mother tongue, and if my questions is silly. With the new network system, I tried to do a little game that goes like this : The player connects to the server, then pick up a hero, click on a button ("Go") and can play.
But here is my problem : the first player connected will see the second player connected's hero while the second player won't see the first player's hero. I know that the fact that the first player see the second's player hero is due to this command (when he clicks on the "Go" button):
public GameObject champ1;
public GameObject champ2;
[Command]
public void CmdChampSelected(int i)
{
switch (i)
{
case 1:
champ1.SetActive(true);
break;
case 2:
champ2.SetActive(true);
break;
}
}
i is the number of the selected champion.
And I don't understand why it won't do anything if I relaunch it with the first player.
The prefab of the player spawned by the NetworkManager contains the two champions, disabled, and one of them is unable when the player click on the "Go" button after making his choice.
I'm trying to understand all the [command], [SerializedField], [ClientCallback] thing so I maybe did a huge mistake in my code, but I can't see where.
By advance, thank you.
Answer by Natos · Jul 03, 2015 at 07:33 AM
Ok, I found the solution ! Finally !
You have to use ClientRpc like that :
Your command will call your ClientRpc
All the actions will be executed in the Rpc
BAM ! It works ! Hope it helped you ! :)
You will maybe have to associate that action to a key or in a Coroutine to synchronise when you want if it doesn't work the first time.
Answer by seanr · Jul 01, 2015 at 03:52 PM
the state of child objects is not included in the network information for spawned objects.
You could put a SyncVar on the object to hold "championId", then in OnStartClient active the champion on the client.
Hello and thank you for your help. I tried it, adding this to the class, and it didn't work. (I don't have any error in the console) Sorry if I miss understood the solution you were proposing.
[SyncVar] private int selectedChampion;
void OnStartClient()
{
selectedChampion = canvas.GetComponent<CharacterChoice>().champSelected;
switch (selectedChampion)
{
case 1:
champ1.SetActive(true);
break;
case 2:
champ2.SetActive(true);
break;
}
}
No, it's a $$anonymous$$onoBehaviour. Should I change it to NetworkBehaviour ? I'm affraid that it will mess with the script.
I changed it to NetworkBehaviour but it still doesn't work.