创建播放器对象不能从构造函数中工作

创建播放器对象不能从构造函数中工作

问题描述:

需要一些专业知识。目前我正在从一个类产卵玩家在创建对象,然后通过线定义变量线,像这样的方式:创建播放器对象不能从构造函数中工作

Player p = new Player(); 
p.Avatar = go; 
p.PlayerName = playerName; 
p.ConnectionId = cnnId; 
p.Avatar.GetComponentInChildren<TextMesh>().text = pName; 
players.Add(cnnId, p); 

和玩家类是一个简单的点点结构是这样的:

public class Player 
{ 
    public string PlayerName; 
    public GameObject Avatar; 
    public int ConnectionId; 
} 

这样的工作,但我想扩大的参数和使用构造函数来创建,我试图通过创建我的对象这样的,而不是做我的播放器对象:

Player p = new Player(playerName, go, cnnId); p.Avatar.GetComponentInChildren<TextMesh>().text = pName; players.Add(cnnId, p);

,然后我创造了我的构造是这样的:

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"; 
    } 
} 

当我尝试使用类的构造方法,虽然它不工作 - 我的球员的名字没有出现,我的球员的跑动没有更新整个网络了。我需要做些什么改变来让我的构造函数工作?

我使用进程派生我的球员

功能齐全:

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); 
    } 
+0

你为什么要用struct?如果你切换到一个班级,它是否能解决你的问题? –

+1

您确定没有收到任何NullReferenceException,尤其是在设置头像游戏对象时? –

+0

@ScottChamberlain切换到类似乎不能解决问题。 – greyBow

当我尝试使用类的构造方法,虽然它不工作 - 我 球员的名字没有出现,我的玩家移动不再通过网络更新 。为了让我的 构造函数能够正常工作,需要做些什么改变?

首先,这对构造函数没有任何帮助。请注意,您的工作Player是一个struct和没有工作的是class。这真的不应该有任何区别。

这里有两个可能的原因,你的新类没有被更新:

。问题是该类中使用的{ get; set; }。 Unity无法序列化/反序列化自动属性。从class中的每个变量中删除它们,它应该可以工作。

。此外,您并未初始化该类中的client变量。在使用它或在其上执行client.infoDisplayText.GetComponent<Text>()之前对其进行初始化。

+0

删除{get;组; }不幸地修复了这个问题。我发布了我用来产生上面我的播放器对象的整个函数。那里还有别的东西,我可能错了吗? – greyBow

+1

请使用您未初始化的变量来阅读我答复的第二部分.... – Programmer

+1

对不起,我错过了那部分。我初始化变量,一切正常。谢谢! – greyBow