- Home /
Send information from one player to another. Unity Multiplayer
I have a player log in with credentials (user name and password), then once they are successfully logged in, other information fields on a script on their player object are filled by my database. I have made it so that if I click on a player a player information box pops up on my screen. I know how to fill the text boxes on the pop up, but how do I fill it with their information? I tried to use RPCs but that didn't work and I am fairly sure they are depreciated now...
Please help.
Where's the information being stored? Database? A component on the player? If it's in a component, you can simply access said player's component as long as the variables are public. If it's a database, what type of database? What script are you using to interface?
Its stored on the player on a script component. and yes, the variable is public. How do I access the information from their player component to appear on my gui text when i click them. Ive already got the text part set up, just need it to show as their info.
Answer by meat5000 · Oct 22, 2015 at 09:36 AM
Came across this info
hi @meat5000, thanks! I was actually just reading up on that too! For your research (aswell) another interesting attribute is: http://docs.unity3d.com/ScriptReference/Networking.SyncVarAttribute.html
SyncVar is mega useful. Its what is used to communicate data between players, such as position and rotation of player characters.
In uJS you use @SyncVar
Answer by Addyarb · Oct 19, 2015 at 08:28 AM
If the information is stored on a component of the player, you can just use a raycasting setup to get that player's collider. So here's how it works in English:
Player A clicks on his screen while his mouse is over Player B.
Player A fires a ray via script at Player B.
Player A's ray hits Player B with said ray, and his [Player A] script returns Player B's collider.
Player A's script converts the returned collider to the collider's gameObject, and then to a component.
Player A gets the component "TheOtherPlayerScript" and then accesses a public variable to populate text objects.
And in script....
void Update () {
GetSelection();
}
void GetSelection() {
// Use Input.GetKeyDown() for single clicks
if(Input.GetKeyDown(KeyCode.Mouse0))
{
Ray ray;
RaycastHit hit;
// Reset ray with new mouse position
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit)) {
TheOtherPlayerScript script = hit.collider.gameObject.GetComponent<TheOtherPlayerScript>();
//Access the public variables from here and you're all set.
}
}
}
Oh okay, well it's a pretty basic thing to access a public variable of another script - but maybe not so much with GUI.
So on line 15, we declared the script on the player we just hit as a temporary variable "script."
So lets say we have a text component that displays that player's name and health.
After line 15 we would write:
(and please replace these string references ASAP)
Text enemyNameDisplay = GameObject.Find("EnemyNameDisplay").GetComponent<Text>();
enemyNameDisplay.text = script.name; //Name is the public variable
float enemyHealthImage = GameObject.Find("EnemyHealthImage").GetComponent<Image>();
enemyHealthImage.fillAmount = script.health/100;
@addyarb yes this just got the local player's info.. not the networked players info. It seems that when a new player connects his info is lost and he takes the local players info or null everything.... how do i make it get the other player?
Place a Cmd function in the script that fires off the players actual info from his system rather than reading the local info that appears on your own machine.