I’m trying to spawn a child object (Has network Identity) of a parent (Doesn’t have network identity). So I’m passing the child of the prefab and it works. Spawning works just fine but it doesn’t go through my spawnhandler, neither on server nor on clients (“custom spawner” is never printed in other words).
So spawned objects are only the childs, and the parent isn’t with them consequently.
In handler, I’m trying to make a parent for the spawned object.
Here’s the relevant codes:
public class CustomNetworkManager : NetworkManager
{
....
public GameObject prefab;
private void Awake()
{
....
ClientScene.RegisterPrefab(prefab.transform.GetChild(0).gameObject);
var prefabAssetId = prefab.transform.GetChild(0).gameObject.GetComponent<NetworkIdentity().assetId;
ClientScene.RegisterSpawnHandler(prefabAssetId , SpawnWithParent, UnSpawnWithParent);
}
public static void Spawn(GameObject prefab)
{
NetworkServer.Spawn(prefab);
}
public GameObject SpawnWithParent(Vector3 position, NetworkHash128 assetId)
{
print("custom spawner");
var parent = Instantiate(new GameObject("parent"), position, Quaternion.identity);
var chlid= Instantiate(prefab.transform.GetChild(0).gameObject);
chlid.transform.position = parent.transform.position;
chlid.transform.parent = parent.transform;
return prefab.gameObject;
}
public void UnSpawnWithParent(GameObject spawned)
{
print("custom unspawner");
spawned.SetActive(false);
}