Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Zahiro · Oct 20, 2016 at 07:57 PM · networkingtextsynchronizationconnection

How to display connections count on client

I have a simple code :

     loadingText.text ="Gathering additional players " + NetworkServer.connections.Count + "/4";

(loadingText is a GUI text element under canvas) but NetworkServer only works for the Server and not for a client (obviously)

Is there a similar solution for clients ?
or
Is there a way i can sync the text component or gameObject to client ?

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

4 Replies

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

Answer by Zahiro · Oct 21, 2016 at 05:17 PM

I used SyncVar to solve the problem

 [SyncVar]
     public int playersConnected;
 
     void Update () {
         if (displayPlayers) {
                     //only server is allowed to announce player count since he is the only that can count them
             if (Network.isServer) {
                 playersConnected = NetworkServer.connections.Count;
             }
             loadingText.text = "Gathering additional players " + playersConnected + "/4";
         }
     }

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 ITEAnonymous · Mar 27, 2017 at 05:19 AM 0
Share

how SyncVar works?

avatar image Zahiro ITEAnonymous · Mar 27, 2017 at 11:30 AM 0
Share

Simplified : It's a variable that is shared over the network session/connection so, for example, if the SyncVar "playersConnected" changes to, let's say, "3" then this variable is "3" for all the connections aka players. !Be aware that ONLY servers(in this case the host) can change a SyncVar so a simple boolean check like "if (Network.isServer)" as the above example is perfect

SyncVar Scripting API reference

avatar image ITEAnonymous Zahiro · Mar 27, 2017 at 12:52 PM 0
Share

where to put the networkbehavior script? when I put the script in the main camera, the camera will disabled,

then where to put the codes? server side or client side? because i separate the server and client

i tried it on client side but returning "0" ins$$anonymous$$d of "1".

then i want to know how client get the value of "playerconnected" if it's change in the server in different script.

lets say server is script A while client is script B. how they can communicate using [syncvar]?

I'm hoping for your help!. thanks

Show more comments
avatar image
2

Answer by Gaidzin · Nov 01, 2017 at 02:37 PM

NetworkServer.connections may be nulls in this list for disconnected clients. It not really count of connections.

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
2

Answer by BjoUnity3d · Jul 08, 2018 at 10:05 PM

As Gaidzin said, Since NetworkServer.connections.Count can have nulls it doesn't give you the actual number of connected clients. This is how I got the actual count.

         int GetConnectionCount()
         {
             int count = 0;
             foreach (NetworkConnection con in NetworkServer.connections)
             {
                 if (con != null)
                     count++;
             }
             return count;
         }
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
1

Answer by sundayhd5 · Jan 20, 2021 at 08:18 PM

This what I did:

 public class NetworkPlayer : NetworkBehaviour
 {
     private static int count = 0;
 
     private void Awake()
     {
         count++;
     }
 
     private void OnDestroy()
     {
         count--;
     }
 
     private void Start()
     {
         if (isLocalPlayer)
         {
             transform.name = "NetworkPlayer (Local)";
         }
         else
             transform.name = "NetworkPlayer (Network)";
 
         print($"{transform.name}, total players: {count}");
     }
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

7 People are following this question.

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

Related Questions

How to fix this: OnObjSpawn netId: 1 has invalid asset Id UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate() ? 2 Answers

Network Send Server Data to Client and Vice Versa 1 Answer

RPC or Synchronization ? 1 Answer

NetworkMatch.DropConnection not executing callback 0 Answers

ScriptableObjects and SyncVars, is it posible? 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