protected void OnDisable() { if (doneSpawning) { rigidbody.velocity = Vector3.zero; if (catParticlePool == null) { catParticlePool = TapGOPoolSingleton <CatParticle> .PoolInstance(); } TapGameObject particle = catParticlePool.GetObject(); if (particle) { particle.gameObject.SetActive(true); particle.GetComponent <CatParticle>().PlayAtLocation(transform.position); } else { Debug.LogError("Could not play particle system"); } gameManager.IncScore(1); popSound.Play(); } doneSpawning = false; base.OnDisable(); }
public static TapGOPool CreatePool(GameObject templatePrefab, int amount) { if (amount <= 0) { Debug.LogError("Cannot create an empty pool!"); return(null); } if (templatePrefab.GetComponent <Template>() == null) { Debug.LogError("Object prefab not of template type!"); return(null); } if (poolInstance != null) { Debug.LogError("GameObjectPool already created!"); return(null); } lock (_createlock) { if (poolInstance == null && activePoolInstance == null) { // create a new instance of TapGOPool for our singleton GameObject newinstance = new GameObject("TapGOPool"); newinstance.AddComponent <TapGOPool>(); poolInstance = newinstance.GetComponent <TapGOPool>(); // create a new instance of ActiveTapGOPool GameObject newactiveinstance = new GameObject("activePool"); newactiveinstance.AddComponent <ActiveTapGOPool>(); activePoolInstance = newactiveinstance.GetComponent <ActiveTapGOPool>(); if (poolInstance == null || activePoolInstance == null) { Debug.LogError("Could not create poolInstance!"); } } else { return(poolInstance); } } // add our objects to the pool for (int i = 0; i < amount; i++) { TapGameObject prefab = (Instantiate(templatePrefab) as GameObject).GetComponent <TapGameObject>(); if (!prefab) { Debug.LogError("Not a TapGameObject!"); } prefab.gameObject.SetActive(false); // initially set object to be inactive prefab.transform.SetParent(poolInstance.transform); // set the created prefab to be a child of the pool object for cleanliness // initialize the activepool for the object // (we do this after setting it to active to make sure we don't call the OneEnable and OnDisable functions) prefab.activeTapGOPool = activePoolInstance; poolInstance.objectList.Add(prefab); } return(poolInstance); }