我正在写一个简单的棋盘游戏使用unity。我需要设置我的对象(Player)的属性,尽管我将其引用为GameObject。我需要选角,但不能。以下是我尝试过的方法
public GameObject PlayerPrefab;
private GameObject player;
// Use this for initialization
void Start () {
if (!isLocalPlayer)
{
return;
}
Debug.Log("Spawning.");
CmdSpawn();
}
[Command]
void CmdSpawn()
{
player = Instantiate(PlayerPrefab);
((Player)player).parentNetId = this.netId;
NetworkServer.SpawnWithClientAuthority(player, connectionToClient);
}
我有以下错误:"Assets/Scripts/PlayerConnectionObject.cs(27,18):error CS0030: Cannot convert type UnityEngine.GameObject' to
Player'“
发布于 2019-02-17 01:53:17
播放器是附加到你的PlayerPrefab上的组件,你应该使用GetComponent<>。还可以使用can存储缓存的PlayerComponent,这是GetComponent运行速度缓慢的原因:
更改字段:
private Player player;
和内部命令:
player = Instantiate(PlayerPrefab).GetComponent<Player>();
player.parentNetId = this.netId;
NetworkServer.SpawnWithClientAuthority(player.gameObject, connectionToClient);
https://stackoverflow.com/questions/54724144
复制相似问题