Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug Spawning object on host only appears on host and not for other clients

Discussion in 'Scripting' started by Cakestax, May 18, 2024.

  1. Cakestax


    Nov 9, 2023
    Hey! So I'm working on a buildmode system for my game and I have run into an issue.

    When using
    Code (CSharp):
    1. NetworkObject.Spawn()
    on the host, it only spawns the object passed on the host not the clients. For example, when a client places an object on their end, it calls a server RPC to which on the server side the object is meant to be spawned for ALL clients.

    Everything building wise works and I have put all of my prefabs used in the NetworkPrefabsList in the NetworkManager.

    Here is the code for the server RPC and the function spawning the object into the game after being placed:

    Code (CSharp):
    2. [ServerRpc (RequireOwnership = false)]
    3. public void PrepareBuildForManipulationServerRpc(string function, string prefabName, Vector3 pos = new Vector3(), int rotationY = 0) {
    4. ManipulateBuild(function, prefabName, pos, rotationY);
    5. }
    7. void ManipulateBuild(string function, string prefabName, Vector3 pos, int rotationY) {
    8. Buildable buildsettings = buildables.Find(prefabName).GetComponent<Buildable>();
    10. if (function == "Spawn") {
    11. GameObject newPrefab = Instantiate(buildsettings.prefab);
    13. Destroy(newPrefab.GetComponent<BoxCollider>()); //Destroy box collider so it does not interfere with prompts etc
    15. NetworkObject networkObject = newPrefab.AddComponent<NetworkObject>(); //Adding network object for syncronisation
    17. newPrefab.transform.SetLocalPositionAndRotation(pos, Quaternion.Euler(0, rotationY, 0));
    18. = prefabName;
    20. networkObject.SpawnWithObservers = true;
    21. networkObject.Spawn(true);
    22. newPrefab.transform.SetParent(GameObject.Find("Builds").transform, true);
    24. } else if (function == "Destroy") {
    25. foreach (Transform build in GameObject.Find("Builds").transform) {
    26. if (build.localPosition == pos) {
    27. Destroy(build.gameObject);
    28. break;
    29. }
    30. }
    31. }
    32. }
    I appreciate all help and also feel free to ask questions!
  2. CodeSmile


    Apr 10, 2014
    This won't work.

    If I'm not mistaken the NetworkObject component has to be already added to the prefab. Without it, it's not a "network" prefab and will not spawn properly. I'm surprised that NGO does not complain neither on startup nor when spawning this object. Or are you getting errors/warnings but ignored them?

    Note that passing a string "function" is wasteful when the same can be done with an enum. It's also likely unnecessary to pass the "prefabName" too because the GameObject name will not be synchronized anywayw. If you need it to be synchronized for debugging purposes, make it a separate RPC method that you only call in editor to reduce the overhead of this RPC.