Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Unity custom spawn handler not calling ?

Discussion in 'Connected Games' started by AbhinayNegi, Jan 8, 2019.

  1. AbhinayNegi

    AbhinayNegi

    Joined:
    Dec 22, 2017
    Posts:
    17
    I followed Unity Custom Spawn Handler to implement Object pooling https://docs.unity3d.com/Manual/UNetCustomSpawning.html. But in my case Spawn Handler function is not working on the client side. So what could be the reasons that Spawn handler function is not working?

    Code: Spawn Manager

    Code (CSharp):
    1. public class SPawn : MonoBehaviour {
    2.     public int m_ObjectPoolSize = 10;
    3.     public GameObject m_Prefab;
    4.     public GameObject[] m_Pool;
    5.  
    6.     public NetworkHash128 assetIDBullet { get; set; }
    7.  
    8.     public delegate GameObject SpawnDelegate(Vector3 position, NetworkHash128 assetId);
    9.     public delegate void UnSpawnDelegate(GameObject spawned);
    10.  
    11.     void Start() {
    12.         assetIDBullet = m_Prefab.GetComponent<NetworkIdentity>().assetId;
    13.         m_Pool = new GameObject[m_ObjectPoolSize];
    14.         for (int i = 0; i < m_ObjectPoolSize; ++i) {
    15.             m_Pool[i] = (GameObject)Instantiate(m_Prefab, Vector3.zero, Quaternion.identity);
    16.             m_Pool[i].name = "PoolObject" + i;
    17.             m_Pool[i].SetActive(false);
    18.         }
    19.  
    20.         ClientScene.RegisterSpawnHandler(assetIDBullet, SpawnObject, UnSpawnObject);
    21.     }
    22.  
    23.     public GameObject GetFromPool(Vector3 position) {
    24.         foreach (var obj in m_Pool) {
    25.             if (!obj.activeInHierarchy) {
    26.                 Debug.Log("Activating GameObject " + obj.name + " at " + position);
    27.                 obj.transform.position = position;
    28.                 obj.SetActive(true);
    29.                 return obj;
    30.             }
    31.         }
    32.         Debug.LogError("Could not grab GameObject from pool, nothing available");
    33.         return null;
    34.     }
    35.  
    36.     public GameObject SpawnObject(Vector3 position, NetworkHash128 assetId) {
    37.         return GetFromPool(position);
    38.     }
    39.  
    40.     public void UnSpawnObject(GameObject spawned) {
    41.         Debug.Log("Re-pooling GameObject " + spawned.name);
    42.         spawned.SetActive(false);
    43.     }
    44. [B]In the previous code The SpawnManager class the Handler function SpawnObject is not working on client but UnSpawnObject is working.[/B]
    45. }
    Code: Player
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4.  
    5. public class Play : NetworkBehaviour {
    6.  
    7.     SPawn spawnManager;
    8.  
    9.     void Start() {
    10.         spawnManager = GameObject.Find("SP Manager").GetComponent<SPawn>();
    11.     }
    12.  
    13.     void Update() {
    14.         if (!isLocalPlayer)
    15.             return;
    16.  
    17.         var x = Input.GetAxis("Horizontal") * 0.1f;
    18.         var z = Input.GetAxis("Vertical") * 0.1f;
    19.  
    20.         transform.Translate(x, 0, z);
    21.  
    22.         if (Input.GetKeyDown(KeyCode.Space)) {
    23.             // Command function is called on the client, but invoked on the server
    24.             CmdFire();
    25.         }
    26.     }
    27.  
    28.     [Command]
    29.     void CmdFire() {
    30.         // Set up coin on server
    31.         var coin = spawnManager.GetFromPool(transform.position - transform.right);
    32.         coin.GetComponent<Rigidbody>().velocity = transform.forward * 4;
    33.  
    34.         // spawn coin on client, custom spawn handler is called
    35.         NetworkServer.Spawn(coin, spawnManager.assetIDBullet);
    36.  
    37.         // when the coin is destroyed on the server, it is automatically destroyed on clients
    38.         StartCoroutine(Destroy(coin, 2.0f));
    39.     }
    40.  
    41.     public IEnumerator Destroy(GameObject go, float timer) {
    42.         yield return new WaitForSeconds(timer);
    43.         spawnManager.UnSpawnObjectBullet(go);
    44.         NetworkServer.UnSpawn(go);
    45.     }
    46. }
    47.  
     
    Last edited: Jan 8, 2019
  2. AbhinayNegi

    AbhinayNegi

    Joined:
    Dec 22, 2017
    Posts:
    17
    Well, I don't know how but, it worked. I simply removed gameObject prefab from NetworkManager's Registered Spawnable Prefab and the SpawnHandler worked. Now can anyone explain to me why this worked?