[英]SpawnObject for ak(Clone) (UnityEngine.GameObject), NetworkServer is not active. Cannot spawn objects without an active server
helloI'm making an object in Unity that gives players random weapons when they hover over it, but it always gives me this warning and doesn't create it.你好我正在 Unity 中制作一个 object,当玩家在 hover 上方时,它会为玩家提供随机武器,但它总是给我这个警告并且不会创建它。
[ClientRpc]
public void spawnTime()
{
StartCoroutine(spawn());
}
public IEnumerator spawn()
{
Debug.Log("oldu");
yield return new WaitForSeconds(1);
int a = Random.Range(0, guns.Length);
GameObject gun =Instantiate(guns[a], spawnPoint.position,Quaternion.identity);
gun.transform.SetParent(transform);
NetworkServer.Spawn(gun);
}
This is because of you are calling the spawn function from client.这是因为您正在从客户端调用 spawn function。 You should call it in server.
您应该在服务器中调用它。
[Server]
public void spawnTime()
{
StartCoroutine(spawn());
}
[Server]
public IEnumerator spawn()
{
Debug.Log("oldu");
yield return new WaitForSeconds(1);
int a = Random.Range(0, guns.Length);
GameObject gun =Instantiate(guns[a],
spawnPoint.position,Quaternion.identity);
gun.transform.SetParent(transform);
NetworkServer.Spawn(gun);
uint gunNetId = gun.GetComponent<NetworkIdentity>().netId;
uint parentNetId = transform.root.GetComponent<NetworkIdentity>().netId;
RpcSetParent(parentNetId, gunNetId);
}
[ClientRpc]
void RpcSetParent(uint parentNetId, uint childNetId)
{
Transform child = NetworkClient.spawned[childNetId].transform;
Transform parent = NetworkClient.spawned[parentNetId].transform;
child.SetParent(parent);
}
Be sure that you are calling the spawnTime() function from something running in server and networked object.确保您从服务器中运行的东西和联网的 object 调用spawnTime() function。 You can filter it something like:
您可以对其进行过滤,例如:
Start(){
NetworkIdentity nid = transform.root.GetComponent<NetworkIdentity>();
if(nid.isServer)
{
spawnTime();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.