2017-10-14 7 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);

,369을

다음이 같은 내 생성자를 만들어 :

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입니다. 이것은 실제로 어떤 차이도 없어야합니다. 새로운 클래스가 업데이트되지 않는 이유

는 다음 두 가지 이유가 있습니다 :

1 국지적 인 문제가 그 클래스에서 { get; set; }의 사용이다. Unity는 자동 속성을 직렬화/비 직렬화 할 수 없습니다. 해당 변수를 각 class에서 제거하면 제대로 작동합니다.

또한 해당 클래스의 client 변수를 초기화하지 않습니다. 그것을 사용하거나 그것에 client.infoDisplayText.GetComponent<Text>()를 수행하기 전에 그것을 초기화하십시오.

+0

제거 {get; 세트; } 불행히도 문제를 해결하지 못했습니다. 위의 플레이어 오브젝트를 생성하는 데 사용하는 전체 기능을 게시했습니다. 그곳에 제가 틀린 다른 것이 있을까요? – greyBow

+1

초기화하지 않은 변수를 사용하여 내 대답 abut의 두 번째 부분을 읽으십시오 .... – Programmer

+1

죄송합니다. 그 부분을 놓쳤습니다. 변수를 초기화하면 모든 것이 작동합니다. 고맙습니다! – greyBow