Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by qasem2016 · Aug 12, 2017 at 07:41 PM · uinetworkingtextvariablesync

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]

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image qasem2016 · Aug 13, 2017 at 09:38 AM 0
Share

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

avatar image qasem2016 · Aug 13, 2017 at 04:40 PM 0
Share

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 ? :/ :\ :|

avatar image unidad2pete qasem2016 · Aug 13, 2017 at 09:48 PM 1
Share

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
          }
  
      }
avatar image qasem2016 · Aug 24, 2017 at 08:07 AM 0
Share

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 ?

avatar image
1

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 ;)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Populate UI Text with appropriate Network View 1 Answer

Why Does Adding to My SyncListStruct Break SyncVar? 0 Answers

SyncVar on objects in a list 1 Answer

Adding a point to a variable is making it not show. 0 Answers

Displaying variable on UI text every frame (JS) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges