Hi everyone,
I’m working on a multiplayer project using Unity’s Netcode for GameObjects, and I’m encountering an issue where I need to disable an object for the host but keep it enabled for the clients. The object is spawned on the server, and I want to ensure that the host sees the object as disabled while the clients do not.
Here is the relevant code snippet:
using Unity.Netcode;
using UnityEngine;
public class MyObjectManager : NetworkBehaviour
{
[SerializeField] private GameObject myObjectPrefab;
public override void OnNetworkSpawn()
{
if (IsServer)
{
// Spawn the object on the server
GameObject myObject = Instantiate(myObjectPrefab);
NetworkObject netObj = myObject.GetComponent<NetworkObject>();
if (netObj != null)
{
netObj.Spawn();
}
// Disable the object for the host
if (IsHost)
{
myObject.SetActive(false);
}
}
}
}
The issue I’m facing is that when the host disables the object, it gets disabled for all clients as well. I want the object to remain active for the clients while being disabled for the host.
Any help or suggestions on how to achieve this would be greatly appreciated. Thank you!