Search Unity

Resolved Override OnTriggerEnter2D Method in Subclass

Discussion in 'Physics' started by siddharth3322, Jan 30, 2023.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    I want override OnTriggerEnter2D method within subclass or derived class I created.

    I have tried as per my knowledge but its not working, I can't able to call super class and sub class method within single collision detection.

    When the object collide, I want callback for super class and derived class OnTriggerEnter2D methods.

    Block Super Class:
    Code (CSharp):
    1. public class Block : MonoBehaviour
    2. {
    3.     [HideInInspector] public int xPosition;
    4.     [HideInInspector] public int yPosition;
    5.     public int health = 5;
    6.     //
    7.     [SerializeField] Color normalColor;
    8.     [SerializeField] Color hitColor;
    9.     //
    10.     Animation blockAnimation;
    11.  
    12.     private void Awake()
    13.     {
    14.         blockAnimation = GetComponent<Animation>();
    15.     }
    16.  
    17.     protected void OnTriggerEnter2D(Collider2D other)
    18.     {
    19.         if (other.CompareTag(GameConstants.TAG_BULLET))
    20.         {
    21.             health--;
    22.             if (health <= 0)
    23.             {
    24.                 // destroy block
    25.                 Destroy(gameObject);
    26.                 //blockSpriterRenderer.DOColor(hitColor, 0.2f).SetLoops(1, LoopType.Incremental).OnComplete(
    27.                 //    () => { Destroy(gameObject); } );
    28.             }
    29.             else
    30.             {
    31.                 // reduce health
    32.                 blockAnimation.Play();
    33.                 //blockSpriterRenderer.DOColor(hitColor, 0.2f).SetLoops(1, LoopType.Incremental).OnComplete(
    34.                 //    () => { health--; });
    35.             }
    36.         }
    37.     }  
    38.  
    39. }
    GoldBlock Sub Class:

    Code (CSharp):
    1. public class GoldBlock : Block
    2. {
    3.  
    4.     [SerializeField] GameObject miningItemPref;
    5.  
    6.     protected void OnTriggerEnter2D(Collider2D other)
    7.     {
    8.        
    9.         if (other.CompareTag(GameConstants.TAG_BULLET))
    10.         {
    11.             Debug.Log("Bullet Hit: " + health);
    12.             GenerateMiningItem();
    13.         }
    14.     }
    15.  
    16.     private void GenerateMiningItem()
    17.     {
    18.  
    19.     }
    20. }
    At present, Sub class's OnTriggerEnter2D() method is getting called.
    Please share your suggestion regarding this.
     
  2. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    797
    siddharth3322 likes this.
  3. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    @flashframe Thank you for your reply, It helped me a lot :)
    Here is my updated code:
    Super Class:
    Code (CSharp):
    1.  
    2. public class Block : MonoBehaviour
    3. {
    4.    
    5.     [HideInInspector] public int xPosition;
    6.     [HideInInspector] public int yPosition;
    7.     public int health = 5;
    8.     //
    9.     //[SerializeField] Color normalColor;
    10.     //[SerializeField] Color hitColor;
    11.     //
    12.     protected Animation blockAnimation;
    13.  
    14.     private void Awake()
    15.     {
    16.         blockAnimation = GetComponent<Animation>();
    17.     }
    18.  
    19.     protected virtual void OnTriggerEnter2D(Collider2D other)
    20.     {
    21.         if (other.CompareTag(GameConstants.TAG_BULLET))
    22.         {
    23.             health--;
    24.             if (health <= 0)
    25.             {
    26.                 // destroy block
    27.                 Destroy(gameObject);
    28.                 //blockSpriterRenderer.DOColor(hitColor, 0.2f).SetLoops(1, LoopType.Incremental).OnComplete(
    29.                 //    () => { Destroy(gameObject); } );
    30.             }
    31.             else
    32.             {
    33.                 // reduce health
    34.                 blockAnimation.Play();
    35.                 //blockSpriterRenderer.DOColor(hitColor, 0.2f).SetLoops(1, LoopType.Incremental).OnComplete(
    36.                 //    () => { health--; });
    37.             }
    38.         }
    39.     }  
    40. }
    41.  
    Sub Class:
    Code (CSharp):
    1.  
    2. public class OilBlock : Block
    3. {
    4.     [SerializeField] GameObject miningItemPref;
    5.  
    6.  
    7.     protected override void OnTriggerEnter2D(Collider2D other)
    8.     {
    9.         base.OnTriggerEnter2D(other);
    10.         if (other.CompareTag(GameConstants.TAG_BULLET))
    11.         {
    12.             if (health <= 0)
    13.             {
    14.                 // generate mining object
    15.                 GenerateMiningItem();
    16.             }
    17.         }
    18.     }
    19.  
    20.     private void GenerateMiningItem()
    21.     {
    22.         Instantiate(miningItemPref, transform.position, Quaternion.identity, transform.parent);
    23.     }
    24.  
    25. }
    26.  
    I hope this will become useful to other members as well.
     
    flashframe likes this.