0

私は現在、このネットワーク全体が一体となってどのように機能するかを学んでいます。私のコードでは、複数のプレハブから作られた宇宙船を作成しています。

すべては 1 つの から始まりHardpointます。Hardpointは、後でループ内でインスタンス化される単一のオブジェクトを保持できます。

(PlayerController開始点) には、最初のオブジェクトであるコックピットを生成する次のコードがあります。

[Command]
void CmdOnConnect() {
    string json = GameObject.Find("TestPlayer").GetComponent<ComponentObject>().ToJSON();
    CompressedComponent compressedComponent = JsonUtility.FromJson<CompressedComponent>(json);
    gameObject.GetComponent<Hardpoint>().Hold(GameObject.Find("Component Repository").GetComponent<ComponentRepository>().cockpit[compressedComponent.componentNumber]);
    gameObject.GetComponent<Hardpoint>().SpawnComponent();
    gameObject.GetComponent<Hardpoint>().RollThroughDecompression(compressedComponent);
    Camera.main.GetComponent<PlayerCamera>().player = gameObject;
}

次は、スクリプトSpawnComponent()内にあるコードです。Hardpoint

public void SpawnComponent() {
    Clear();
    CmdSpawn();
}

CmdSpawn も次の場所にありHardpointます。

[Command]
public void CmdSpawn()
{
    Debug.Log("[COMMAND] Spawning " + holds.name);
    heldInstance = Instantiate(holds, transform.position, transform.rotation) as GameObject;
    heldInstance.transform.SetParent(transform);
    NetworkServer.SpawnWithClientAuthority(heldInstance, transform.root.gameObject);
}

そして最後RollThroughDecompressionに、関数を呼び出すだけDecompress()です:

public void RollThroughDecompression(CompressedComponent c) {
    heldInstance.GetComponent<ComponentObject>().Decompress(c);
}

そして、情報を除外しないために、次のようにしDecompress()ます。

public void Decompress(CompressedComponent c) {
    componentType = (Type)Enum.Parse(typeof(Type), c.componentType);
    componentNumber = c.componentNumber;
    UpdateHardPoints();
    GameObject[] typeRepository = GetRepository(componentType);

    //update children 
    int point = 0;
    foreach (Transform child in transform)
    {
        Hardpoint hardpoint = child.GetComponent<Hardpoint>();
        if (hardpoint != null) {
            if (c.hardpoints[point] != null) {
                //get the hardpoint's repository
                GameObject[] hardpointRepo = GetRepository((Type)Enum.Parse(typeof(Type), c.hardpoints[point].componentType));
                //set the hardpoint to hold this object
                hardpoint.Hold(hardpointRepo[c.hardpoints[point].componentNumber]);
                hardpoint.SpawnComponent();
                hardpoint.RollThroughDecompression(c.hardpoints[point]);
                point++;
            }
        }
    }
}

申し訳ありませんが、コードが少し乱雑/混乱していますが、最初に生成されたオブジェクトを除いて、新しく生成されたオブジェクトが持たない理由を突き止めようとして、壁を追い上げてきclient authorityました (おそらく から呼び出されたためPlayerController)。私は何日もこの問題に悩まされてきました。新しくスポーンされたオブジェクトは、ローカル プレイヤー オブジェクトの子として設定されNetworkServer.SpawnWithClientAuthority、テスト時にまだスポーンされます。

Trying to send command for object without authority.呼び出すときCmdSpawn()

NetworkManager: ここに画像の説明を入力

私が得ている結果:

ここに画像の説明を入力

ご覧のとおり、コックピット (一番最初の部分) が期待どおりに生成されます。しかし、それらに取り付けられた部品はそうでHardpointsはありません。明確にするために、それEmptyHardpointはまさにそれです。子のないハードポイントで、hardpointスクリプトとplayercontrollerアタッチされた空のゲーム オブジェクトです。コックピットプレハブにはimgとも含まれていますhardpoints

4

1 に答える 1