I’m new to networking, so I’m sure this is a basic problem that has a simple solution, but I cannot figure it out.
When I start as host a player prefab is created. I then find that player prefab and rename it. The issue I’m having is that the name change isn’t showing for my client when it connects. It should be named “MyPrefab”, but it’s still keeping its basic name “PlayerPrefab(Clone).”
The PlayerPrefab prefab has the NetworkedObject component. Here’s the inspector:
The code for starting my host and updating the player object is:
Host/Client Start and object renaming code:
public void StartGameAsHost()
{
Debug.Log("Starting game as host...");
NetworkingManager.Singleton.StartHost();
InitializePlayerObject();
//NetworkSceneManager.SwitchScene(SPData.SCENEGameSetup);
}
public void StartGameAsClient()
{
Debug.Log("Joining game as client...");
NetworkingManager.Singleton.OnClientConnectedCallback += OnClientJoins;
NetworkingManager.Singleton.StartClient();
}
private void InitializePlayerObject()
{
Player p = new Player(playerName); //Generate a player object
GameObject g = GameObject.Find(SPData.PREFABPlayer + "(Clone)");
try
{
g.name = playerName + " Player";
}
catch (System.Exception ex)
{
Debug.LogError("Player object not found.\n" + ex.Message);
}
PlayerPrefabManager ppm = g.GetComponent<PlayerPrefabManager>();
ppm.Init(p);
}
public void OnClientJoins(ulong id)
{
Debug.Log($"Client joined: {id}");
InitializePlayerObject();
}
PlayerPrefabManager: (Basic, nothing much to it yet)
using MLAPI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerPrefabManager : NetworkedBehaviour
{
[Header("Player Reference:")]
public Player player;
[Header("Component References:")]
public PlayerUIHandler playerUIHandler;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void Init(Player p)
{
player = p;
playerUIHandler.SetPlayerName(p.Name);
}
}
PlayerUIHandler:
using MLAPI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerUIHandler : NetworkedBehaviour
{
[Header("UI References")]
public Text playerName;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void SetPlayerName(string n)
{
playerName.text = n;
}
}
Player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player
{
private string playerName;
public string Name { get => playerName; private set { } }
public Player(string n)
{
playerName = n;
}
}
As I said, I’m sure this is something simple, but I could really use some help.
I’m not sure if it’s needed, but just in case, here’s the full code of the LobbyUIHandler script that contains all of my client code. There’s a lot of debugging and sequencing stuff in there since I’m still trying to figure out how networking works.
using MLAPI;
using MLAPI.SceneManagement;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LobbyUIHandler : NetworkedBehaviour
{
[Header("UI References")]
public InputField fieldName;
[Space(10)]
public Button buttonHost;
public Button buttonJoin;
private string playerName;
public void Start()
{
try
{
playerName = PlayerPrefs.GetString(SPData.PREFPlayerName);
} catch (System.Exception ex)
{
Debug.LogError(ex.Message);
playerName = "";
}
fieldName.text = playerName;
UpdateButtonState();
//DEBUG
NetworkSceneManager.OnSceneSwitchStarted += OnStartSceneSwitch;
NetworkSceneManager.OnSceneSwitched += OnSceneSwitched;
}
public void OnDestroy()
{
//DEBUG
NetworkSceneManager.OnSceneSwitchStarted -= OnStartSceneSwitch;
}
public void OnNameChanged()
{
playerName = fieldName.text;
PlayerPrefs.SetString(SPData.PREFPlayerName, playerName);
UpdateButtonState();
}
private void UpdateButtonState()
{
buttonHost.interactable = !string.IsNullOrEmpty(playerName);
buttonJoin.interactable = !string.IsNullOrEmpty(playerName);
}
public void StartGameAsHost()
{
Debug.Log("Starting game as host...");
NetworkingManager.Singleton.StartHost();
InitializePlayerObject();
//NetworkSceneManager.SwitchScene(SPData.SCENEGameSetup);
}
public void StartGameAsClient()
{
Debug.Log("Joining game as client...");
NetworkingManager.Singleton.OnClientConnectedCallback += OnClientJoins;
NetworkingManager.Singleton.StartClient();
}
private void InitializePlayerObject()
{
Player p = new Player(playerName); //Generate a player object
GameObject g = GameObject.Find(SPData.PREFABPlayer + "(Clone)");
try
{
g.name = playerName + " Player";
}
catch (System.Exception ex)
{
Debug.LogError("Player object not found.\n" + ex.Message);
}
PlayerPrefabManager ppm = g.GetComponent<PlayerPrefabManager>();
ppm.Init(p);
}
public void OnStartSceneSwitch(AsyncOperation operation)
{
//Debug.Log("The scene should have started switching now.");
}
public void OnSceneSwitched()
{
Debug.Log("The scene has finished switching.");
}
public void OnClientJoins(ulong id)
{
Debug.Log($"Client joined: {id}");
InitializePlayerObject();
}
}
SPData is just a class that contains persistent strings and other data like that.