- Home /
show sync var on two text ui (unet - networking)
ok im tired :((
i have a 1 v 1 game and a scrip on them have a score var . the score are sync and i want to show it in two ClientText and ServerText UI Text ! each device show it's score but not other one .
how can i show the sync var of each one of them on two text ui on their own device ?
[sorry for my bad english]
Answer by unidad2pete · Aug 12, 2017 at 08:18 PM
Hi, First, SyncVar there are been changed via Command, and you need tell all clients your change via Rpc. You have some options, and you dont need SyncVars.
public class Example : NetworkBehaviour
{
// Example without Syncvar
public int myScore;
public Text scorePlayer1;
public Text scorePlayer2;
// In this case, is Player1
public void ScoreUP()
{
myScore += 10;
scorePlayer1.text = myScore.ToString();
//We call function on server, send it our score
CmdScoreUp(myScore);
}
[Command]
public void CmdScoreUp(int score)
{
// Server say all clients, your score
RpcScoreUp(score);
}
[ClientRpc]
public void RpcScoreUp(int score)
{
// You dont need do this action again, will be do it only your instance on all clients
if (!isLocalPlayer)
{
myScore = score;
scorePlayer1.text = score.ToString();
}
}
}
If you dont understand the mecanism, share your code to adapt.
it works but how can i change scorePlayer2 ?
i try but i fail !
can you change the script ?
also i have a question that i ask it after your respond :)
thanks for your answer
never $$anonymous$$d :D i get it work by tag ! in my ui button i do this :
GameObject Player = GameObject.FindGameObjectWithTag ("Player");
if (Player != null) {
Score score = Player.GetComponent<Score> ();
score.ScoreUP ();
}
now i'm confuse :/
i quote from unity manual :
Commands are sent from player objects on the client to player objects on the server.
ClientRpc calls are sent from objects on the server to objects on clients .
So . lets say i join a game , am i server and others client or who start the game are server and others client ? :/ :\ :|
You are Server and client, but you are local player on your pc, and client on other pc, each player is local player of they pc.
I Try to explain.
[img]http://i.imgur.com/CwRJqlS.png[/img]
In this case, both players have a script, with score variable to 100; if you changes your variable to 200, on your PC, your variable is 200, but in other PCs, your variable is 100. When you uses Rpc, your function is applied only on your player, but on all PCs. When you uses Command, the function is applied on server, but not on all PCs. SynVar have exception, they changed by server, and automaticaly server send like a Rpc call to send the info on all PCs.
if, for example, you change UI text with a RPCcall
[RpcClient]
public void RpcScorePlayer1()
{
TextScorePlayer1.text = score;
}
On all pcs, the UI text change to show the score , in this case, you are who calls function, then score is your score, but if on your PC, your score is 100, and on other PC your score is 200, UI text on your PC = 100, and 200 in others.
Rpc calls, can not called by clients, only by the server, but client can call Command, and inside that command, a Rpc call, all code in that Rpc function, will be applied on your player, on all PCs, but only on your player, to info others players your actual score.
Try this, in the first code I shared with you
[ClientRpc]
public void RpcScoreUp(int score)
{
// You dont need do this action again, will be do it only your instance on all clients
if (!isLocalPlayer)
{
myScore = score;
scorePlayer1.text = score.ToString();
}
}
Your are changed your UI text on your pc on score Player 1, this call only says other players do the same, but if you change the scorePlayer1 to scorePlayer2, on your pc, your score will be scorePlayer1 and your oponent score will be scorePlayer2, and inverse on other PC
[ClientRpc]
public void RpcScoreUp(int score)
{
// You dont need do this action again, will be do it only your instance on all clients
if (!isLocalPlayer)
{
myScore = score;
scorePlayer2.text = score.ToString(); // HERE
}
}
hello , its me again :D
http://answers.unity3d.com/questions/1396918/solution-to-my-problem-need-your-experience-in-une.html
i have new question about unet but no one answered ! do you know what should i do ?
Answer by qasem2016 · Oct 18, 2017 at 10:48 AM
hey @unidad2pete
how are you doing ? :D :p ME AGAIN :D :))
i am still struggling with rpc and cmd (after few month pause) :|
ok , do you remember my problem ? i show "myScore" variable on text ui with your codes , now i want send my "point" !
in every playercontroller i have a "point" variable . if player wins it add up with 1 !
i want to every player update "point" variable when a player wins !
sorry for calling you <3 i am confused in unet functions :/ another guiding from you should solve everything ;)
Answer by vanesamoreno41 · Dec 02, 2020 at 12:33 AM
Hi,
I have used your code because I have a problem and the texts overlap. My composition is an empty object and within that empty object the canvas with the text, and this composition is added to prebafs in NetworkManager. I don't know how to fix this.