Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在创建无法从构造函数运行的播放器对象_C#_Unity3d_Constructor - Fatal编程技术网

C# 正在创建无法从构造函数运行的播放器对象

C# 正在创建无法从构造函数运行的播放器对象,c#,unity3d,constructor,C#,Unity3d,Constructor,这方面需要一些专业知识。我目前正在从一个类中生成一个玩家,创建对象,然后逐行定义变量,如下所示: 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 {

这方面需要一些专业知识。我目前正在从一个类中生成一个玩家,创建对象,然后逐行定义变量,如下所示:

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;
}
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";
    }
}
这是可行的,但我想扩展参数并使用构造函数来创建我的播放器对象,我试图通过创建我的对象来实现这一点,如下所示:

Player p=新玩家(playerName、go、cnnId);
p、 Avatar.getComponentChildren().text=pName;
玩家。添加(cnnId,p)

然后我创建了我的构造函数,如下所示:

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;
}
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";
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共结构播放器{
客户;
公共字符串播放器名称{get;set;}
公共游戏对象化身{get;set;}
public int ConnectionId{get;set;}
公共字节[]Tex{get;set;}
公共字符串类型{get;set;}
公共字符串Id{get;set;}
公共整数强度{get;set;}
公共int生命点{get;set;}
公共bool IsAlive{get;set;}
//初始方法采用基本参数进行测试
公共玩家(字符串玩家名称、游戏对象化身、int connectionID):this()
{
this.PlayerName=PlayerName;
这个。阿凡达=阿凡达;
this.ConnectionId=ConnectionId;
client.infoDisplayText.GetComponent().text+=playerName++++ConnectionId++\n;
}
//重载方法接受所有播放器参数
公共玩家(字符串玩家名称、游戏对象化身、int-connectionID、字节[]tex、字符串类型、字符串id、int-strength、int-hitpoints、bool-isAlive):this()
{
this.PlayerName=PlayerName;
这个。阿凡达=阿凡达;
this.ConnectionId=ConnectionId;
这个.Tex=Tex;
this.Type=Type;
这个.Id=Id;
这个。力量=力量;
Hitpoints=Hitpoints;
this.IsAlive=IsAlive;
Debug.Log(id+”:“+type+”,创建时使用强度“+strength+”、生命点“+hitpoints+”,以及大小为“+tex.Length”的纹理);
client.infoDisplayText.GetComponent().text+=playerName+“”+id+:“+type+”创建时使用强度“+strength+”、命中点“+hitpoints+”,以及大小为“+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);
    }
private void spownplayer(字符串pName,int cnnId)
{
GameObject go=实例化(playerPrefab)为GameObject;
//这是我们的球员吗?
如果(cnnId==ourClientId)
{
//增加流动性
go.AddComponent();//添加Movement.cs脚本
//卸下连接按钮
if(GameObject.Find(“Canvas”).activeInHierarchy==true)
GameObject.Find(“ConnectButton”).SetActive(false);
IsStart=true;
}
玩家p=新玩家(玩家名称、围棋、cnnId);
p、 Avatar.getComponentChildren().text=pName;
玩家。添加(cnnId,p);
}
当我尝试使用构造函数方法时,尽管它不起作用-我的 玩家名称不会出现,我的玩家移动也不会更新 再也不能通过网络了。我需要做哪些更改才能获得我的 建造师要工作吗

首先,这与构造函数没有任何关系。请注意,正在工作的
播放器
是一个
结构
,而不工作的是一个
。这真的没什么区别

以下是未更新新类的两个可能原因:

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


2。此外,您没有初始化该类中的
客户端
变量。在使用它或对其执行
client.infoDisplayText.GetComponent()
之前初始化它。

为什么要使用结构?如果你切换到一个类,它能解决你的问题吗?你确定你没有收到任何NullReferenceException,特别是在设置avatar游戏对象时?@ScottChamberlain切换到一个类似乎不能解决问题。不幸的是,删除{get;set;}并不能解决问题。我在上面发布了我用来生成我的玩家对象的整个函数。这里面还有什么我可能弄错了吗?请阅读我答案的第二部分,关于使用一个你没有初始化的变量…对不起,我错过了那部分。我初始化了变量,一切正常。非常感谢。