question about photonview.owner.name

Options
TillyT
edited March 2013 in DotNet
I'm probably in over my head trying to build a multiplayer game, but I'm trying to get each avatar's name to appear above their head. Is photonview.owner.name the best way to achieve this? If so, can you help me understand why the following code doesn't work? This script is attached to the instantiated Player prefab, which also has a Photon View. It doesn't throw any errors, but the textmesh remains blank at runtime.

Any help would be greatly appreciated. Thanks!

[code2=csharp]using UnityEngine;
using System.Collections;

public class PlayerName : MonoBehaviour {

public Transform playerPrefab;
public string PlayerUserName;

void Start () {
PlayerUserName = playerPrefab.transform.GetComponent<PhotonView>().owner.name;
playerPrefab.transform.Find("PlayerName").GetComponent<TextMesh>().text = (PlayerUserName);
}
}[/code2]

Comments

  • Tobias
    Options
    You are using the playerPrefab. This gets instantiated but the actual new game object in the scene is another (instance).

    [code2=csharp]void Start()
    {
    string PlayerUserName = this.transform.GetComponent<PhotonView>().owner.name;
    Debug.Log("Name: " + PlayerUserName);
    this.transform.Find("PlayerName").GetComponent<TextMesh>().text = (PlayerUserName);
    }[/code2]
  • TillyT
    Options
    I see - that makes sense.

    However, the TextMesh still stays empty, and doesn't display a name. Where is the actual owner value coming from? Is it the instantiated player prefab's name? Or the owner named in the Photon View? It looks like the Photon View owner value is set to [1] on instantiation.

    Any other ideas why this isn't working? Thanks again!
  • TillyT
    Options
    I'm still stuck on this - tried a couple of other ideas, but no matter what I try every player is named after the local player since, as you said, they're all instances. I'm finally wrapping my mind around that. ;-)

    So I'm back to trying to figure out why when I use PhotonView.owner.name the name stays blank when I assign it to the textmesh. Do I have to assign an owner.name before using it?
  • Tobias
    Options
    Each player should set a username. You can set it even before you connect (but also later on, even in game).
    PhotonNetwork.playerName
    This way, each player in a room sets it's name. Per photonview, the owner (a player) is known and you should be able to show that user's name.
    The "[1]" points at "having no name" and shows the user's actornumber instead.
  • TillyT
    Options
    Thanks Tobias, I really appreciate your help.

    So I have a "username" variable from my registration system script in my registration scene.

    If I set that username to PhotonNetwork.playerName, all players see the same local player's name above all the players. How do I get that username variable assigned to be the Photon View owner name when it's instantiated in the game scene?

    Thanks in advance! Again, I *really* appreciate your help.
  • Tobias
    Options
    I thought I explained above. Each player should assign his own name to PhotonNetwork.playerName. Before joining a game.
    Then, this gets synced in the room. Use PhotonNetwork.Instantiate() to instantiate game objects per player. They belong to that player and on all clients, the photonView.owner.name should be the one of the user who instantiated the game object.
  • TillyT
    Options
    Got it! Thanks again for your help.