Search Unity

How to call a protected method?

Discussion in 'Scripting' started by Deeeds, Jun 18, 2018.

  1. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
  2. 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.
     
  3. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
    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?
     
  4. Make it public and call it from anywhere you want.
     
  5. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
    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.
     
  6. Could you please post your code, what are you trying to do? Please, use the code tag here on the forum.
     
  7. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PoolObject : MonoBehaviour {
    5.  
    6.     public virtual void OnObjectReuse() {
    7.  
    8.     }
    9.  
    10.     protected void Destroy() {
    11.         gameObject.SetActive (false);
    12.     }
    13. }
     
  8. I saw this, could you please post the code where you're trying to call this?
     
  9. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class CubeScript : PoolObject {
    5.  
    6. void Update () {
    7.         transform.localScale += Vector3.one * Time.deltaTime * 5;
    8.         transform.Translate (Vector3.forward * Time.deltaTime * 25);
    9.         if (transform.position.z > 10)     // --- things tried here below ---///
    10.         //Destroy(this);
    11.         //PoolObject.Destroy(this);
    12.         //Destroy(gameObject);
    13.         // PoolObject.Destroy(gameObject);
    14.        
    15.         //// the only one that works...
    16.         gameObject.SetActive(false);
    17.  
    18. etc...
     
  10. Replace this with this:
    Code (CSharp):
    1. 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.
     
    Deeeds likes this.
  11. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
    THANK YOU!!!

    No way I was going to figure that out.

    Far too obvious!
     
    Lurking-Ninja likes this.