smartfox hovering usernames

Hi comm unity!

Im neither scripter nor unity wizz, but have managed to set up a small scene with a chat with loads of help from a smartfox island tutorial.

A lot is still abracadabra to me…, but learning :slight_smile:

my question is:

I have instantiated players with a GUIText attached and a script named ObjectLabel that makes the GUItext follow the player object.

that works very nice, but it would be so cool to have the usernames hovering above the player object
and more precise…, dynamically the one that they logged on with smartfox.

How do I get the username that they logged on with in smartfox to that hovering GUIText?

I know the username data must be somewhere,
but not able to send it to the GUIText :frowning:

I see this in the chatController.cs script where the username shows up in chat:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

using SmartFoxClientAPI;
using SmartFoxClientAPI.Data;

public class ChatController : MonoBehaviour {
	
	public GUISkin skin;
	
	private Vector2 scrollPosition;
	
	private List<String> messages = new List<String>();
	
	private Rect chatWindow;
	private string userMessage = "";
	
	private bool typingMessage = false;
	private bool sendingMessage = false;
		
		
	void Start() {
		//chatWindow = new Rect(Screen.width-Screen.width, Screen.height - Screen.height/5, Screen.width /2.5f, Screen.height/5);
	}


	void OnGUI() {
		GUI.skin = skin;
		
		chatWindow = new Rect(Screen.width-Screen.width, Screen.height - Screen.height/1.5f, Screen.width /4.4f, Screen.height/1.5f);
		if (EnterPressed()) {
			if (!typingMessage) {
				 typingMessage = true;
			}
			else {
				// Send message
	   	    	if (userMessage.Length > 0) {
					AddMyChatMessage(userMessage);
					userMessage = "";
				}
				typingMessage = false;
			}
			Event.current.Use();	 
		}	
		
		chatWindow = GUI.Window (1, chatWindow, ShowChatWindow, "Party Island");
	}
	
	private bool EnterPressed() {
		return (Event.current.type == EventType.keyDown  Event.current.character == '\n');
	}
	
	void ShowChatWindow(int id) {	
		GUI.SetNextControlName("scroll"); 
		scrollPosition = GUILayout.BeginScrollView (scrollPosition);
		foreach(string message in messages) {
			GUILayout.BeginHorizontal();
			GUILayout.Label(message);
			
			GUILayout.FlexibleSpace();
			GUILayout.EndHorizontal();
			GUILayout.Space(3);
		}
				
	    GUILayout.EndScrollView();
	   	if (!typingMessage) {
	   		 GUI.FocusControl("scroll");
	   	}
	   	
	   	GUI.SetNextControlName("text"); 
	   	userMessage = GUILayout.TextField(userMessage);
	   	if (typingMessage) {
	   		 GUI.FocusControl("text");
		}
		
		GUILayout.Label("Press Enter to type and again to send");
	}
	
	private void AddMyChatMessage(String message) {
		SmartFoxClient client = NetworkController.GetClient();
		String userName = client.myUserName;
		AddChatMessage(userName+": "+message);
		SendChatMessage(message);
	}
	
	// This method to be called when remote chat message is received
	void AddChatMessage(String message) {
		messages.Add(message);
		scrollPosition.y = 10000000000; // To scroll down the messages window
	}
		
	// Send the chat message to all other users
	private void SendChatMessage(String message) {
		SmartFoxClient client = NetworkController.GetClient();
		client.SendPublicMessage(message);	
	}
}

can someone give me an example please?
must be beginner proof :sweat_smile:
Thanks

Are you trying to get your username to float above your own head, or enemy usernames to float above their heads?

im having the same problem i have the GUI text hovering above my LocalPlayer Prefab but i cant get the client.myUsername var from Smartfox Script into that GUI text. I guess once i’ve done it for the local player it will be easy to do it for the network player.

Post the script segment of you trying to display that text so I can actually see if it’s an error in the coding or etc.

For example, from what you said above, client.myUsername is incorrect. it’s should read client.myUserName

ok i have a GUIText in my Hierarchy with the following script attached to it.

import System;
import SmartFoxClientAPI;
import SmartFoxClientAPI.Data;
import SmartFoxClientAPI.Util;
import SmartFoxClientAPI.Http; 

var target : Transform;  // Object that this label should follow
//var username : String =  dataObject.GetString("myUsername") ; 
var offset = Vector3.up;    // Units in world space to offset; 1 unit above object by default
var clampToScreen = false;  // If true, label will be visible even if object is off screen
var clampBorderSize = .05;  // How much viewport space to leave at the borders when a label is being clamped
var useMainCamera = true;   // Use the camera tagged MainCamera
var cameraToUse : Camera;   // Only use this if useMainCamera is false
private var cam : Camera;
private var thisTransform : Transform;
private var camTransform : Transform;

function Start () {
    thisTransform = transform;
    if (useMainCamera)
        cam = Camera.main;
    else
        cam = cameraToUse;
    camTransform = cam.transform;
}

function Update () {
	//nameTag.Text = username;
    if (clampToScreen) {
        var relativePosition = camTransform.InverseTransformPoint(target.position);
        relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
        thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
        thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),
                                         Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),
                                         thisTransform.position.z);
    }
    else {
        thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
    }
}

@script RequireComponent(GUIText)

How do i now display the username in this script to my GUI Text? btw this is javascript[/code]

How can we retrieve the logged in usersnames in a smartfox environment?

second step is to apply them to the GUIText :slight_smile: