Need some expertise on this one. I am currently Spawning a player from a class in a way where I create the object and then define the variables line by line, like this:
Player p = new Player();
p.Avatar = go;
p.PlayerName = playerName;
p.ConnectionId = cnnId;
p.Avatar.GetComponentInChildren<TextMesh>().text = pName;
players.Add(cnnId, p);
and the player class was a simple little bit structured like this:
public class Player
{
public string PlayerName;
public GameObject Avatar;
public int ConnectionId;
}
So that worked, but I wanted to expand the arguments and use a constructor to create my player object which I tried to do by creating my object like this instead:
Player p = new Player(playerName, go, cnnId);
p.Avatar.GetComponentInChildren<TextMesh>().text = pName;
players.Add(cnnId, p);
and then I created my constructor like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public struct Player {
Client client;
public string PlayerName { get; set; }
public GameObject Avatar { get; set; }
public int ConnectionId { get; set; }
public byte[] Tex { get; set; }
public string Type { get; set; }
public string Id { get; set; }
public int Strength { get; set; }
public int Hitpoints { get; set; }
public bool IsAlive { get; set; }
// Initial method takes base arguments for testing
public Player(string playerName, GameObject avatar, int connectionID) : this()
{
this.PlayerName = playerName;
this.Avatar = avatar;
this.ConnectionId = connectionID;
client.infoDisplayText.GetComponent<Text>().text += playerName + " " + ConnectionId + " " + "\n";
}
// Overload method takes all player arguments
public Player(string playerName, GameObject avatar, int connectionID, byte[] tex, string type, string id, int strength, int hitpoints, bool isAlive) : this()
{
this.PlayerName = playerName;
this.Avatar = avatar;
this.ConnectionId = connectionID;
this.Tex = tex;
this.Type = type;
this.Id = id;
this.Strength = strength;
this.Hitpoints = hitpoints;
this.IsAlive = isAlive;
Debug.Log(id + " : " + type + " created with strength " + strength + ", hit points " + hitpoints + ", and a texture the size of " + tex.Length);
client.infoDisplayText.GetComponent<Text>().text += playerName + " " + id + " : " + type + " created with strength " + strength + ", hit points " + hitpoints + ", and a texture the size of " + tex.Length +" \n";
}
}
When I try to use the constructor method though it doesn't work - my player name doesn't appear and my player movement doesn't get updated across the network anymore. What changes do I need to make to get my constructor to work?
Complete function that I'm using to Spawn my player:
private void SpawnPlayer(string pName, int cnnId)
{
GameObject go = Instantiate(playerPrefab) as GameObject;
// Is this our player?
if (cnnId == ourClientId)
{
// Add mobility
go.AddComponent<Movement>(); // Add Movement.cs Script
// Remove Connect Button
if(GameObject.Find("Canvas").activeInHierarchy == true)
GameObject.Find("ConnectButton").SetActive(false);
isStarted = true;
}
Player p = new Player(playerName, go, cnnId);
p.Avatar.GetComponentInChildren<TextMesh>().text = pName;
players.Add(cnnId, p);
}