Hi, I have a doubt. I’m using Unity Netcode, and I have a script that helps sync data from a scene object. The problem is that I want to instantiate the object as a prefab, destroy it, and then re-instantiate it later. However, when I destroy the prefab, the associated NetworkObject is also getting destroyed, which is causing issues. How can I solve this problem?
#Networking issue
1 messages · Page 1 of 1 (latest)
Generally don't want to be destroying scene objects. If they need to be removed at any point, just spawn them manually when the network session begins.
Also just to be clear about terminology, you should never be destroying objects with a NetworkObject component manually. The server should be despawning them.
Calling Destroy() on the server/host will also despawn the networkobject. But yea, if a networkobject needs to be Despawned then it also needs to be Spawned first.
You could also use Object Visibility if you need to hide the object from certain clients
Object (NetworkObject) visibility is a Netcode for GameObjects term used to describe whether a NetworkObject is visible to one or more clients as it pertains to a netcode/network perspective. When a NetworkObject is visible to a client, the server will assure the client has a spawned version (a clone) of the NetworkObject. This also means that...
One more thing — my Network Prefab is not instantiated when the game starts. I'm making sure that the application is running as Host or Server, but the NetworkObject is not visible, so the server cannot detect it. The server requires this object to be present in the scene. How can I solve this issue?
You'll need to make sure the object is also active and enabled in the scene when it loads. If you are switching to the scene after starting the server, then be sure to use NetworkManager.SceneManager.LoadScene()