- Home /
Question by
cj31387 · Jan 04, 2014 at 09:49 AM ·
networkingmultiplayerphotonnetworkview
Photon - How to name a player?
Apparently PhotonNetwork.playerName is what I need to use. But if i instantiate a player through photon its a game object, I can't then change the player name because its of type PhotonNetwork not gameobject. So how do you assign a player name to a specific player when they join? Heres my code.
using UnityEngine;
using System.Collections;
public class MPManager : Photon.MonoBehaviour
{
public GameObject[] spawnPoints;
// Use this for initialization
void Start()
{
spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
PhotonNetwork.ConnectUsingSettings("Aftermath Alpha 0.4");
//int spawnRand = Random.Range(0, spawnPoints.Length);
}
void OnGUI()
{
GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
}
void OnJoinedLobby()
{
PhotonNetwork.JoinRandomRoom();
}
void OnPhotonRandomJoinFailed()
{
PhotonNetwork.CreateRoom("Aftermath - AW");
}
void OnJoinedRoom()
{
//555 Stupid inconsistency with random.range, it starts at 1 instead of 0.
GameObject myPlayer = PhotonNetwork.Instantiate("Player_MP", spawnPoints[Random.Range(1, spawnPoints.Length)].transform.position, spawnPoints[Random.Range(1, spawnPoints.Length)].transform.rotation, 0);
string playerNumber = Random.Range(1, 1000).ToString();
myPlayer.GetComponent<PhotonView>().owner.name = playerNumber;
myPlayer.name = "Player" + playerNumber;
}
void OnDisconnectedFromPhoton()
{
//PhotonNetwork.DestroyPlayerObjects(photonView.ownerId);
//print(photonView.ownerId + " objects destroyed from server");
}
}
Comment