Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 LegendWill · Mar 19, 2015 at 09:47 AM · c#networkusername

Multiplayer Usernames c#

I'm having a lot of trouble synchronizing a player's username to the other player. At the start of my game, they are asked to give their self a username that will be used for that session and it stores it in a PlayerStats script that is on an empty game object. When a player joins a lobby(maximum of 2 players), their lobby name is set by the username that is in the PlayerStats script. I need to find a way to get the other player's username to store it a LobbyHandler string.

alt text


LobbyHandler script -

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class LobbyHandler : MonoBehaviour {
     public string playerName;
     public Text playerOneName;
     public Text playerTwoName;
     public bool isLobbyPlayerOne;
     public bool isLobbyPlayerTwo;
     public GameObject Scripts;
 
     public string playerOneNameString;
     public string playerTwoNameString;
     void Awake () {
         Scripts = GameObject.Find("_Scripts");
         playerOneName.text = "<Loading>";
         playerTwoName.text = "<Waiting>";
     }
     void Start () {
         playerName = GameObject.Find("_Scripts").GetComponent<PlayerStats>().Username;
     }
     [RPC] void OnPlayerConnected(NetworkPlayer player) {
         StartCoroutine(SetLobbyStats());
         ConsoleLog.Instance.Log(playerName + " joined.");
     }
     public void SetLobbyStatsFunction () {
         StartCoroutine(SetLobbyStats());
     }
     [RPC] IEnumerator SetLobbyStats () {
         yield return new WaitForSeconds(1);
         if (Scripts.GetComponent<PlayerStats> ().isPlayerOne == true) {
             isLobbyPlayerOne = true;
             isLobbyPlayerTwo = false;
             AssignPlayerOne();
             UpdatePlayerNames();
         } else {
             isLobbyPlayerOne = false;
             AssignPlayerTwo();
             isLobbyPlayerTwo = true;
             UpdatePlayerNames();
         }
     }
     [RPC] void UpdatePlayerNames() {
         //
     }
 
     [RPC] void AssignPlayerOne () {
         playerOneNameString = Scripts.GetComponent<PlayerStats>().Username;
         playerOneName.text = Scripts.GetComponent<PlayerStats>().Username;
     }
 
     [RPC] void AssignPlayerTwo () {
         playerTwoNameString = Scripts.GetComponent<PlayerStats>().Username;
         playerTwoName.text = Scripts.GetComponent<PlayerStats>().Username;
     }
 }
 



PlayerStats Script -

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerStats : MonoBehaviour {
 
     public string Username = "Unassigned";
     public bool isPlayerOne = false;
     public bool isPlayerTwo = false;
 
     public void CheckUsername () {
         if (Username == "") {
             Username = "Unassigned";
         }
     }
 }



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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eluate · Mar 19, 2015 at 09:52 AM

When you are calling RPC methods, you should be using NetworkView.RPC.

 networkView.RPC("AssignPlayerTwo", RPCMode.AllBuffered, viewID, transform.position);

You need to set your code up so that the RPC methods are being called by NetworkView.RPC.

Comment
Add comment · Show 6 · 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 LegendWill · Mar 19, 2015 at 05:22 PM 0
Share

I updated my script, but it sets player one and player two's names to the person who is playing. I mean like, player one sees player one's name as both player one and player two. Vice versa for player two. What did I do wrong?


 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class LobbyHandler : $$anonymous$$onoBehaviour {
     public string playerName;
     public Text playerOneName;
     public Text playerTwoName;
     public bool isLobbyPlayerOne;
     public bool isLobbyPlayerTwo;
     public GameObject Scripts;
     public string playerOneNameString;
     public string playerTwoNameString;
     void Awake () {
         Scripts = GameObject.Find("_Scripts");
         playerOneName.text = "<Loading>";
         playerTwoName.text = "<Waiting>";
     }
     void Start () {
         playerName = GameObject.Find("_Scripts").GetComponent<PlayerStats>().Username;
     }
     [RPC] void OnPlayerConnected(NetworkPlayer player) {
         StartCoroutine(SetLobbyStats());
         ConsoleLog.Instance.Log(playerName + " joined.");
     }
     public void SetLobbyStatsFunction () {
         StartCoroutine(SetLobbyStats());
     }
     [RPC] IEnumerator SetLobbyStats () {
         yield return new WaitForSeconds(1);
         if (Scripts.GetComponent<PlayerStats> ().isPlayerOne == true) {
             isLobbyPlayerOne = true;
 
             NetworkViewID viewID = Network.AllocateViewID();
             GetComponent<NetworkView>().RPC("AssignPlayerOne", RPC$$anonymous$$ode.AllBuffered, viewID, transform.position);
 
             isLobbyPlayerTwo = false;
             UpdatePlayerNames();
         } else {
             isLobbyPlayerOne = false;
             NetworkViewID viewID = Network.AllocateViewID();
             GetComponent<NetworkView>().RPC("AssignPlayerTwo", RPC$$anonymous$$ode.AllBuffered, viewID, transform.position);
             isLobbyPlayerTwo = true;
             UpdatePlayerNames();
         }
     }
     [RPC] void UpdatePlayerNames() {
         //later
     }
 
     [RPC] void AssignPlayerOne (NetworkViewID viewID, Vector3 location) {
         playerOneNameString = Scripts.GetComponent<PlayerStats>().Username;
         playerOneName.text = Scripts.GetComponent<PlayerStats>().Username;
     }
     
     [RPC] void AssignPlayerTwo (NetworkViewID viewID, Vector3 location) {
         playerTwoNameString = Scripts.GetComponent<PlayerStats>().Username;
         playerTwoName.text = Scripts.GetComponent<PlayerStats>().Username;
     }
 }
 

avatar image Eluate · Mar 19, 2015 at 08:03 PM 0
Share

You need to make it so that in the RPC call (since it runs on both clients) that the player you are assigning to includes the if statement to check whether he is player one or two.

avatar image LegendWill · Mar 19, 2015 at 10:26 PM 0
Share

I got rid of some of the unneeded stuff. The result is still the same. Perhaps there is something wrong with my Network $$anonymous$$anager?(at bottom of this comment)

Lobby Handler

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class LobbyHandler : $$anonymous$$onoBehaviour {
     public Text playerOneName;
     public Text playerTwoName;
     public GameObject Scripts;
     public string playerOneNameString;
     public string playerTwoNameString;
     void Awake () {
         Scripts = GameObject.Find("_Scripts");
         playerOneName.text = "<Loading>";
         playerTwoName.text = "<Waiting>";
     }
     [RPC] void OnPlayerConnected(NetworkPlayer player) {
         NetworkViewID viewID = Network.AllocateViewID();
         GetComponent<NetworkView>().RPC("SetLobbyStats", RPC$$anonymous$$ode.AllBuffered, viewID, transform.position);
     }
     public void SetLobbyStatsFunction () {
         StartCoroutine(SetLobbyStats());
     }
     [RPC] IEnumerator SetLobbyStats () {
         yield return new WaitForSeconds(1);
         if (Scripts.GetComponent<PlayerStats> ().isPlayerOne == true) {
             playerOneNameString = Scripts.GetComponent<PlayerStats>().Username;
             playerOneName.text = Scripts.GetComponent<PlayerStats>().Username;
         } else {
             playerTwoNameString = Scripts.GetComponent<PlayerStats>().Username;
             playerTwoName.text = Scripts.GetComponent<PlayerStats>().Username;
         }
     }
 }
avatar image LegendWill · Mar 19, 2015 at 10:26 PM 0
Share

Network $$anonymous$$anger

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Network$$anonymous$$anager : $$anonymous$$onoBehaviour {
 
     public const string typeName = "102398471982374987";
     public const string gameName = "RoomName";
     public GameObject playerPrefab;
     public GameObject $$anonymous$$ain$$anonymous$$enuCanvas;
     public GameObject LobbyCanvas;
     public GameObject StartupCanvas;
     private HostData[] hostList;
 
     void Awake () {
         StartupCanvas.SetActive(true);
     }
 
     void StartServer () {
         Network.InitializeServer(2, 25000, !Network.HavePublicAddress());
         $$anonymous$$asterServer.RegisterHost(typeName, gameName);
     }
     
     void OnServerInitialized()
     {
         SpawnPlayer();
         GetComponent<LobbyHandler>().SetLobbyStatsFunction();
     }
 
     private void RefreshHostList()
     {
         $$anonymous$$asterServer.RequestHostList(typeName);
     }
     
     void On$$anonymous$$asterServerEvent($$anonymous$$asterServerEvent msEvent)
     {
         if (msEvent == $$anonymous$$asterServerEvent.HostListReceived)
             hostList = $$anonymous$$asterServer.PollHostList();
     }
 
     private void JoinServer(HostData hostData)
     {
         Network.Connect(hostData);
         $$anonymous$$ain$$anonymous$$enuCanvas.SetActive(false);
         LobbyCanvas.SetActive(true);
         GameObject.Find("_Scripts").GetComponent<PlayerStats>().isPlayerOne = false;
         GameObject.Find("_Scripts").GetComponent<PlayerStats>().isPlayerTwo = true;
         GetComponent<LobbyHandler>().SetLobbyStatsFunction();
     }
     
     void OnConnectedToServer()
     {
         SpawnPlayer();
     }
 
     private void SpawnPlayer()
     {
         Network.Instantiate(playerPrefab, new Vector3(0f, 5f, 0f), Quaternion.identity, 0);
     }
 
     public void CreateButtonFunction() {
         StartServer();
         LobbyCanvas.SetActive(true);
         $$anonymous$$ain$$anonymous$$enuCanvas.SetActive(false);
         GameObject.Find("_Scripts").GetComponent<PlayerStats>().isPlayerOne = true;
         GameObject.Find("_Scripts").GetComponent<PlayerStats>().isPlayerTwo = false;
     }
     public void JoinButtonFunction() {
         RefreshHostList();
     }
     void OnGUI()
     {
         if (!Network.isClient && !Network.isServer)
         {
             if (hostList != null)
             {
                 for (int i = 0; i < hostList.Length; i++)
                 {
                     if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), hostList[i].gameName))
                         JoinServer(hostList[i]);
                 }
             }
         }
     }
 }
avatar image Eluate · Mar 20, 2015 at 06:21 AM 0
Share

You are using RPC calls incorrectly. The RPC call is called on all other clients so the line

 if (Scripts.GetComponent<PlayerStats> ().isPlayerOne == true) {

does nothing.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Desync between host and client. 0 Answers

Show Photon username based on Distance 0 Answers

Understanding Network.Instantiate 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