I’m currently having an issue with prefabs not spawning on the client when each of my prefabs are added to the registered spawn-able prefabs via scripting immediately on connection. I know they are registered on the client because they show up in the network manager.
and when the client connects to the server and attempts to spawn in the objects the server spawned in, I get the following error for each and every object. In this scenario(the mission config files which provide the information where to spawn in each and every object and how) there was just one:
this is the script that goes through and adds the units to the registered spawnable prefabs:
public void Start ()
{
//adds units to registered spawnable prefabs
GameObject[] units = Resources.LoadAll<GameObject>("Units/");
foreach (GameObject unit in units)
{
FindObjectOfType<NetworkManager>().spawnPrefabs.Add(unit);
}
}
I need to know how do I get the server to sync up the registered prefabs on each and every client when they connect.
Hey, I’ve used my little tool for adding prefabs to the NetworkManager, you just simply need to add a name of the folder and it will run LoadAll functions in Editor Mode which will add prefabs to the script and then attach them to Manager, the only cons is that you need to press the button again if you have added a new prefab to the Resources folder.
Here is how it’s looks:
In order to make it work you need 2 scripts and 1 method.
Put CustomEditors.cs in your Editor folder:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class CustomEditors
{
[CustomEditor(typeof(CustomPrefabsRegisterTool))]
public class CustomPrefabsRegister : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
CustomPrefabsRegisterTool myScript = (CustomPrefabsRegisterTool)target;
EditorUtility.SetDirty(target);
if (GUILayout.Button("Load prefabs"))
{
myScript.RegisterSpawnablePrefabsFromResources();
}
}
}
}
}
Then simply put CustomPrefabsRegisterTool.cs anywhere you want inside your project folder:
using UnityEngine;
using System.Collections.Generic;
[System.Serializable]
public class CustomPrefabsRegisterTool : MonoBehaviour {
[Header("Load from Path Setup")]
public string[] LoadPaths;
public List<GameObject> LoadedPrefabs = new List<GameObject>();
public void RegisterSpawnablePrefabsFromResources()
{
LoadedPrefabs.Clear();
foreach (string str in LoadPaths)
{
GameObject[] objects = Statics.LoadGameObjectsFromResources(str);
foreach (GameObject prefab in objects)
{
LoadedPrefabs.Add(prefab);
}
}
}
}
Then simply drag & drop CustomPrefabsRegisterTool.cs to your NetworkManager object, then you should see something like this
You can add LoadPaths strings to the array. For example, if your folder path is Resources/Units/, just write Units - it will load all gameObjects from Units folder, then click Load Prefabs (you don’t need to manually add GameObjects to the Loaded prefabs array, the script will do it instead of you)
After you done with this, you simply need to add a method to your CustomNetworkManager or singleton object.
Although I do appreciate the reply, for what I am needing, this solution won’t suffice. I need the prefabs to be added at runtime of the game. Reason being is so people can mod the game by simply “dragging and dropping” a mod into a folder and the game would load it. With the solution you gave me, you would need to modify the game inside the editor and essentially make another game which is not what I am pushing for. The solution would need to be similar to GMOD where you just add your mods to the game’s directory and the game just starts up with it loaded. Another thing that would need to be considered is if you connected to a server that is running mod ‘a’ and the client is running mods ‘a’ and ‘b’, the client would need to disable mod ‘b’ and connect.
Did you find a way for doing this ? Im trying to play around with ClientScene.RegisterPrefab ( myPrefab ) but doesnt seems to work… Or do I use it badly?
I think you didn’t include a custom resource loading class for this call ‘Statics.LoadGameObjectsFromResources(str);’. Do you mind adding it to your post, I was hoping to make use of this tool Thank you.
I think that is an issue for dynamic loaded prefabs. I have such a situation say, 100 prefabs are prepared and loaded only 20 of them at the runtime with
spawnPrefabs.Add(prefab)
then spawned with
NetworkServer.Spawn(prefab);
When a new player joins, loaded prefab Ids are shared with message as an array and client also loads them with spawnPrefabs.Add(prefab) however there is no time to spawn them… How to solve it any ideas?