MLAPI - Lost with instantiate
I'm new to Unity development, but not new to development. I'm looking to build a multiplayer framework over the next few years that I can then start to use to build multiplayer first-person-viewpoint games from within it.
I've completed both of the tutorials; HelloWorld & GoldenPath. Neither of them reference where the instantiate component is, or how/why it currently creates a player prefab object at client connect when there's no script written in the tutorial that covers this.
I suspect its something to do with the pre-built scripts, but they're mindblowing to read through at the moment when I'm just starting out. I simply want my camera object to be attached to my prefab, and have a unique camera for each player... I can't find any good answers for Unity 2020.3 with MLAPI.
For reference, my camera object has been made into a prefab now:
PlayerCamera (Prefab Asset)
Camera: Disabled
Audio Listener: Disabled
NetworkObject: NetworkObject.cs
Script: PlayerEnableComponents.cs
PlayerEnableComponents.cs:
using UnityEngine;
using MLAPI;
public class PlayerEnableComponents : NetworkBehaviour
{
public Camera Cam;
public AudioListener Listener;
private void Start()
{
if (!IsLocalPlayer) return;
Cam.gameObject.SetActive(true);
Listener.gameObject.SetActive(true);
}
}
EDIT: For reference, I've taken this from the MLAPI guide:
Scene Objects# Any objects in the scene with active NetworkObject components will get automatically replicated by MLAPI. There is no need to manually spawn them.
There are two modes that define how scene objects are synchronized.
SoftSync# SoftSync is the default and recommended mode for synchronizing scene objects.
When using SoftSync MLAPI will just synchronize existing scene objects with each other. This allows scene objects to be non prefabs and they will not be replaced, thus keeping their serialized data.
The above does not occur in my project. New items are created, but they are not being 'Soft Synced' as each spawn of the new prefab replaces the last for all players (and the server).
Answer by GetLitGames · Jul 26, 2021 at 12:07 PM
If you configured the network manager to use a default prefab like the web page describes, that prefab will be instantiated any time a new client connects. So you don't need to instantiate anything for a player that connects. If you have your PlayerEnableComponents script on your player prefab, it should work fine.
As described on this page: https://docs-multiplayer.unity3d.com/docs/develop/tutorials/helloworld/helloworldintro/index.html
for future objects, ones that are not set as the default player prefab, it should work as you expect - instantiating anything that is a NetworkBehavior will replicate itself automatically. If you have any problems or you need something more mature/stable, or fits your requirements better you should look at Photon Bolt.