- Home /
Not able to convert GetComponent ().netId to a string.
Here's my code snippet:
public override void OnStartClient () {
base.OnStartClient();
string _netID = GetComponent<NetworkIdentity> ().netId.ToString();
Player _player = GetComponent<Player> ();
GameManager.RegisterPlayer (_netID, _player);
}
When I run the code, the error says that I can't convert the netID to a string. Why not? Is there a workaround? All I need is a string that gives a number (i.e. 1, 2, 3, etc.) based upon which player it is.
Thanks in advance.
Comment
Have you tried using the Value property?
string _netID = GetComponent<NetworkIdentity> ().netId.Value.ToString ();
That works! However, now they both work :/
It now won't stop saying
Exception in OnStartClient:Object reference not set to an instance of an object at Game$$anonymous$$anager.RegisterPlayer (System.String _netID, .Player _player) [0x0001a] in /Users/samueluyemura/Unity/Turret Shooter/Assets/Scripts/Game$$anonymous$$anager.cs:13
at PlayerSetup.OnStartClient () [0x00032] in /Users/samueluyemura/Unity/Turret Shooter/Assets/Scripts/PlayerSetup.cs:33
at UnityEngine.Networking.NetworkIdentity.OnStartClient () [0x0008d] in /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:375
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
I don't have a clue what that means, but thank you!