In a pooling video and github, here: https://github.com/SebLague/Object-Pooling/blob/master/PoolObject.cs I can't figure out where this Destroy should be called. Sorry for such a stupid question. Not a CS grad.
Protected means it's only available in the class itself and any child-class, in other words, if you have a descendant class, which inherits from it, you can call that Destroy inside that class. I haven't seen Sebastian's video(s) about pooling, so I'm a little bit guessing here, but I think you will need to create your poolable objects as a descendant of this Pool object and when you would destroy the object, you call the Destroy method, which simply disable the object instead of really destroy it. Although I probably wouldn't make it protected, I would make it public, because normally the Destroy method is public, so you can call it from the manager class. But this depends on what you want to achieve exactly and how.
Ok. Interesting. I can't descend from two classes, so that's not the way here. And the method isn't defined with a parameter for an object, so how do I call it?
That's not working correctly. The object being passed in is not getting the method's override or what it's termed. It's actually destroying the object instead of deactivating it.
Could you please post your code, what are you trying to do? Please, use the code tag here on the forum.
I'd like to call this Destroy method, and have it work as it's supposed to, ie not destroy the object, but deactivate it: Code (CSharp): using UnityEngine; using System.Collections; public class PoolObject : MonoBehaviour { public virtual void OnObjectReuse() { } protected void Destroy() { gameObject.SetActive (false); } }
Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class CubeScript : PoolObject { void Update () { transform.localScale += Vector3.one * Time.deltaTime * 5; transform.Translate (Vector3.forward * Time.deltaTime * 25); if (transform.position.z > 10) // --- things tried here below ---/// //Destroy(this); //PoolObject.Destroy(this); //Destroy(gameObject); // PoolObject.Destroy(gameObject); //// the only one that works... gameObject.SetActive(false); etc...
Replace this with this: Code (CSharp): Destroy(); Explanation: the Destroy(Object obj) does exist on all Monobehaviours. The PoolObject creates the parameterless Destroy method. You need to call it without parameter in order to call the Destroy method defined in the PoolObject class, the parametered method will call the MonoBehaviour's Destroy method, which will destroy your gameobject. Hope it's clear enough, it's already 11pm here on Sunday, I'm a little bit tired already to explain things clearly in English. Sorry about it.