2017-10-14 3 views
0

これについていくつかの専門知識が必要です。私は現在、私は、オブジェクトを作成する方法で、クラスのプレーヤーを産卵し、行で、変数のラインを定義し、このようにしています:コンストラクタから作業していないプレーヤーオブジェクトを作成する

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

なぜ構造体を使用していますか?クラスに切り替えると問題が解決されますか? –

+1

特にGameObjectを設定しているときに、NullReferenceExceptionが発生していないことを確認してください。 –

+0

@ScottChamberlainはクラスに切り替えることで問題を解決できないようです。 – greyBow

答えて

1

に私はそれが動作しませんが、コンストラクタメソッドを使用しよう - 私の プレイヤー名が表示され、私はありませんプレーヤーの動きはもはやネットワーク上で更新されません。 私の コンストラクタを動作させるためにはどのような変更が必要ですか?

まず、これはコンストラクタには何もありません。あなたの仕事Playerstructであり、働いていない人はclassです。これは本当に違いはありません。 【選択問題は、そのクラスの{ get; set; }の使用です

:ここ

は、新しいクラスが更新されていない2つの理由です。 Unityは自動プロパティをシリアル化/デシリアライズできません。それを各変数から削除してください。それはうまくいくはずです。

また、そのクラスのclient変数を初期化していません。使用する前に初期化するか、client.infoDisplayText.GetComponent<Text>()を実行してください。

+0

{get;セット; }残念ながら問題を修正しませんでした。私は上記の私のプレーヤーオブジェクトを生成するために使用している関数全体を投稿しました。おそらくそこには私が間違っている何か他のものがありますか? – greyBow

+1

初期化していない変数を使用して、私の答えの2番目の部分をお読みください.... – Programmer

+1

すみません、私はその部分を逃しました。私は変数を初期化し、すべてが機能しています。ありがとうございました! – greyBow

関連する問題