이 게임에 대한 전문 지식이 필요합니다.플레이어 개체가 생성자에서 작동하지 않음 만들기
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