Hey! So I’m working on a buildmode system for my game and I have run into an issue.
When using 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:
[ServerRpc (RequireOwnership = false)]
public void PrepareBuildForManipulationServerRpc(string function, string prefabName, Vector3 pos = new Vector3(), int rotationY = 0) {
ManipulateBuild(function, prefabName, pos, rotationY);
}
void ManipulateBuild(string function, string prefabName, Vector3 pos, int rotationY) {
Buildable buildsettings = buildables.Find(prefabName).GetComponent<Buildable>();
if (function == "Spawn") {
GameObject newPrefab = Instantiate(buildsettings.prefab);
Destroy(newPrefab.GetComponent<BoxCollider>()); //Destroy box collider so it does not interfere with prompts etc
NetworkObject networkObject = newPrefab.AddComponent<NetworkObject>(); //Adding network object for syncronisation
newPrefab.transform.SetLocalPositionAndRotation(pos, Quaternion.Euler(0, rotationY, 0));
newPrefab.name = prefabName;
networkObject.SpawnWithObservers = true;
networkObject.Spawn(true);
newPrefab.transform.SetParent(GameObject.Find("Builds").transform, true);
} else if (function == "Destroy") {
foreach (Transform build in GameObject.Find("Builds").transform) {
if (build.localPosition == pos) {
Destroy(build.gameObject);
break;
}
}
}
}
I appreciate all help and also feel free to ask questions!