简体   繁体   English

SpawnObject for ak(Clone) (UnityEngine.GameObject),NetworkServer 未激活。 没有活动服务器就无法生成对象

[英]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.

相关问题 NetworkServer未激活。 没有活动的服务器无法生成对象 - NetworkServer is not active. Cannot spawn objects without an active server 网络服务器未激活。 在 Unity 3D 中没有活动服务器时无法生成对象 - NetworkServer is not active. Cannot spawn objects without an active server in Unity 3D 无法将类型“UnityEngine.Transform”转换为“UnityEngine.GameObject” - Cannot convert type `UnityEngine.Transform' to `UnityEngine.GameObject' 无法从“UnityEngine.GameObject”转换为“System.Predicate”<unityengine.gameobject> '</unityengine.gameobject> - Cannot convert from 'UnityEngine.GameObject' to 'System.Predicate<UnityEngine.GameObject>' 无法将类型“UnityEngine.GameObject”隐式转换为“GameObject” - Cannot implicitly convert type 'UnityEngine.GameObject' to 'GameObject' foreach语句无法遍历UnityEngine.GameObject类型的变量 - foreach statement cannot ooperate on variables of type UnityEngine.GameObject 尝试实例化 object 时无法将类型“UnityEngine.GameObject”隐式转换为“UnityEngine.Vector2” - Cannot implicitly convert type 'UnityEngine.GameObject' to 'UnityEngine.Vector2'" While trying to Instantiate an object 如何将类型“int”转换为“unityengine.gameobject”? - How to convert type 'int' to 'unityengine.gameobject'? SerializationException:类型UnityEngine.GameObject未标记为Serializable - SerializationException: Type UnityEngine.GameObject is not marked as Serializable 错误CS0021:无法将带有[]的索引应用于类型为&#39;UnityEngine.GameObject&#39;的表达式 - Error CS0021: Cannot apply indexing with [] to an expression of type `UnityEngine.GameObject'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM