您好,我已经解决了这个问题3天了,我无法弄清楚它有什么问题。我收到的错误消息是 “SpawnObject for Player(克隆)(UnityEngine.GameObject),NetworkServer未激活。无法在没有活动服务器的情况下生成对象。” 我知道服务器正常启动,因为当我使用客户端连接到它时,关闭服务器,客户端说服务器可能已关闭。这是我到目前为止的代码。 感谢您提前
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;
public class MENU_CONTROLLER : NetworkBehaviour
{
public GameObject ipField;
public GameObject connectButton;
public GameObject hostButton;
public GameObject player;
public Text ip;
public Text PlayerCount;
private int playercount = 0;
void Update() {
}
void vanishMenu() {
ipField.SetActive(false);
connectButton.SetActive(false);
hostButton.SetActive(false);
}
public void ServerConnect() {
Network.Connect(ip.text.ToString(), 4444);
vanishMenu();
SpawnPlayer();
}
public void SpawnPlayer() {
GameObject p = (GameObject)Instantiate(player, transform.position,transform.rotation);
NetworkServer.Spawn(p);
}
public void ServerStart() {
Network.InitializeServer(30, 4444, true);
Network.Connect("localhost", 4444);
SpawnPlayer();
}
void OnPlayerConnected(NetworkPlayer player) {
playercount++;
print("PLAYER CONNECTED");
print(player.ipAddress);
PlayerCount.text = playercount.ToString();
}
}
答案 0 :(得分:0)
您必须从“is server”块中调用NetworkServer.Spawn()。
if (is Server) {
//do your thing to spawn
}