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
0
Question by Sinncere · Apr 18, 2010 at 10:35 PM · networkingplayerarraystextmeshname

Textmesh Username for networkplayers. help.

Hello im looking for a solution, so far i have managed to use a textmesh component that is attached to and object, which is a child of the instantiated prefab, therefore each user has this textmesh object. I have edited code based on chat scripts to make arrays of all users. This script works for two connections, both can see eachothers names above thier avatars head but when the third connects serverside remote 1 and 2 swap usernames and on clients remote 1 & 2 have the same username, lookin for advanced advice on how i can make this work. Thanks.

Namespace..

public var UserName: GameObject; public var Cam: GameObject; public var player : NetworkPlayer; public var username : String; public var awakeName: String; public var remoteUsers : int; public var remoteInt : int = 1; public var userList = new Array(); public var userName: String;

class UserNode{ var userName : String; var networkPlayer : NetworkPlayer; } //

body..

function Awake () { userName = ConnectGui.playerNameInput; // this is a PlayerPrefs.GetString in ConnectGui script. awakeName = userName;

if(networkView.isMine){ gameObject.tag = "LocalName"; gameObject.GetComponentInChildren(TextMesh).text = awakeName; }

 if(!networkView.isMine){
 gameObject.tag = "RemoteName";

}

 RemoteNameInst (); //renames this object from username to unsername1 ...2 ...3 etc.

 if(Network.isClient || Network.isServer){
 networkView.RPC ("TellOurUser", RPCMode.OthersBuffered, awakeName);

}

networkView.RPC("MeshName", RPCMode.OthersBuffered, awakeName);}

 function Update () {

remoteUsers = userList.length;
UserName = GameObject.FindWithTag("LocalName"); UserName.transform.localPosition = Vector3(0, 2, 0); Cam = GameObject.FindWithTag("MainCamera"); var direction : Vector3 = Cam.transform.position - UserName.transform.position; transform.LookAt(UserName.transform.position - direction);

 }

Other functions ...

function OnPlayerDisconnected(player: NetworkPlayer) { userList.Remove(GetUserNode(player)); } function OnNetworkInstantiate (info : NetworkMessageInfo){ remoteInt +=1; RemoteNameInst (); } @RPC //Sent by newly connected clients, recieved by server function TellOurUser(name : String, info : NetworkMessageInfo){ var newEntry : UserNode = new UserNode(); newEntry.userName=name; newEntry.networkPlayer=info.sender; userList.Add(newEntry); }

 @RPC
 function MeshName (name : String){

     for(var entry : UserNode in userList){
        if(entry.userName==name){
           LocalUserName (name); // function bellow changes remotes username textmesh.name
        }
     }
 }

 function GetUserNode(networkPlayer : NetworkPlayer){
      for(var entry : UserNode in  userList){
          if(entry.networkPlayer==networkPlayer){
             return entry;
          }
      }
      Debug.LogError("GetUserNode: Requested a playernode of non-existing player!");
      return null;
 }


 function LocalUserName (name : String){

    var remotelist = gameObject.FindGameObjectsWithTag ("RemoteName");

    if(!gameObject.Find("Username2")){
      remotelist[0].GetComponent(TextMesh).text = name;
    }    
    if(gameObject.Find("Username1") && gameObject.Find("Username2")){
      remotelist[1].GetComponent(TextMesh).text = name;
    }
    if(gameObject.Find("Username1") && gameObject.Find("Username2")&& gameObject.Find("Username3")){
      remotelist[2].GetComponent(TextMesh).text = name;
    }

    return;
  }


 function RemoteNameInst (){
     var rgo = gameObject.FindGameObjectsWithTag ("RemoteName");

     if(!networkView.isMine){
       if(remoteInt == 1){
          rgo[0].name = "Username" + 1;
       }
       if(remoteInt == 2){
          rgo[0].name = "Username" + 1;
          rgo[1].name = "Username" + 2;
       }
       if(remoteInt == 3){
          rgo[0].name = "Username" + 1;
          rgo[1].name = "Username" + 2;
          rgo[2].name = "Username" + 3;
       }
     }
 }

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
Best Answer

Answer by Sinncere · Apr 19, 2010 at 01:28 PM

nevermind, ive managed to create a workin script for this problem. Any improvements are welcome.

few edits ...

RPCMode's ...

@RPC function TellOurUser(name : String, info : NetworkMessageInfo){ var newEntry : UserNode = new UserNode(); newEntry.userName=name; newEntry.networkPlayer=info.sender; userList.Add(newEntry); PlayerPrefs.SetString(name, name); // <---- creating keys for remote usernames. }

@RPC function MeshName (name : String){ for(var entry : UserNode in userList){ if(entry.userName==name){
var userPrefs = PlayerPrefs.GetString(name, "");
LocalUserName (userPrefs); // <-- if remote name == the username in our array, then userPrefs = string key name from playerPrefs.
} } }

related functions ...

function LocalUserName (name : String){ var remotelist = gameObject.FindGameObjectsWithTag ("RemoteName"); if(gameObject.Find("Username1")){ var un1 : GameObject = gameObject.Find("Username1"); if(un1.GetComponent(TextMesh).text.Length ==0){ remotelist[0].GetComponent(TextMesh).text = name; } } if(gameObject.Find("Username2")){ var un2 : GameObject = gameObject.Find("Username2"); if(un2.GetComponent(TextMesh).text.Length ==0){ remotelist[1].GetComponent(TextMesh).text = name; } } if(gameObject.Find("Username3")){ var un3 : GameObject = gameObject.Find("Username3"); if(un3.GetComponent(TextMesh).text.Length ==0){ remotelist[2].GetComponent(TextMesh).text = name; } }

return; }

function RemoteNameInst (){ var rgo = gameObject.FindGameObjectsWithTag ("RemoteName"); if(!networkView.isMine){ if(remoteInt == 1){ rgo[0].name = "Username" + 1; } if(remoteInt == 2){ rgo[0].name = "Username" + 1; rgo[1].name = "Username" + 2; } if(remoteInt == 3){ rgo[0].name = "Username" + 1; rgo[1].name = "Username" + 2; rgo[2].name = "Username" + 3; }

} }

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

No one has followed this question yet.

Related Questions

TextMesh multiplayer/network issue 0 Answers

TextMesh sync player names with other players 1 Answer

Deleting a player over the network 1 Answer

How to Get a Char from his name ? 1 Answer

Assigning Player Numbers 0 Answers


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