これについていくつかの専門知識が必要です。私は現在、私は、オブジェクトを作成する方法で、クラスのプレーヤーを産卵し、行で、変数のラインを定義し、このようにしています:コンストラクタから作業していないプレーヤーオブジェクトを作成する
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);
}
なぜ構造体を使用していますか?クラスに切り替えると問題が解決されますか? –
特にGameObjectを設定しているときに、NullReferenceExceptionが発生していないことを確認してください。 –
@ScottChamberlainはクラスに切り替えることで問題を解決できないようです。 – greyBow