Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Run derived method instead of the superclass method

Discussion in 'Scripting' started by Gokushivum, Sep 24, 2020.

  1. Gokushivum

    Gokushivum

    Joined:
    Oct 21, 2014
    Posts:
    7
    So sorry for the dumb sounding title, but I'm not completely sure how to word this question. But in my game, I have a superclass called weapon which handles the basic equipping the weapon off the ground and I am going to make multiple scripts for dealing with different weapons since the firing method won't be the same between them since some 'guns' may not 'shoot' bullets. But I have the weapon being saved to a weapon gameobject on the player and when I click the mouse, it should call the fire method. I would want it to call the fire method no matter which script is attached.
    I was thinking that maybe if I used a superclass, I could only call the method from the super class instead of having the check which weapon script the gun has then call that specific method.
    Am I thinking about this right? I'm new to scripting for Unity.

    Here is the code for picking up the weapon but I haven't started to write the other weapon scripts yet
    Code (CSharp):
    1. if(other.tag == "Weapon")
    2.         {
    3.             var tempWeapon = Instantiate(other, weaponEquipSlot.transform.position, Quaternion.identity);
    4.             tempWeapon.transform.parent = gameObject.transform;
    5.             tempWeapon.GetComponent<Weapon>().equipWeapon();
    6.             equippedWeapon = tempWeapon.gameObject;
    7.             Destroy(other.gameObject);
    8.         }
    But I expect when I write the code for firing, it would be something simple where I would call the equipped weapon's fire method.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Yep - it's pretty simple.

    On Weapon, define a virtual method:
    Code (CSharp):
    1. public class Weapon : MonoBehaviour {
    2.   public virtual void Fire() { }
    3.  
    4.   void Update() {
    5.     if (Input.GetMouseButton(0)) {
    6.       Fire();
    7.     }
    8.   }
    9. }
    Then in the subclasses you just override it:

    Code (CSharp):
    1. public class Bazooka : Weapon {
    2.   public override void Fire() {
    3.     // Do bazooka-specific stuff here.
    4.   }
    5. }
    Alternatively you can make Weapon an abstract class so that it's only possible to have instances of subclasses, and not possible to have an instance of a plain "Weapon". In that case there's just a couple changes:

    Make weapon and Fire abstract, and don't provide any implementation for Fire():
    Code (CSharp):
    1. public abstract class Weapon : MonoBehaviour {
    2.   public abstract void Fire();
    3.  
    4.   void Update() {
    5.     if (Input.GetMouseButton(0)) {
    6.       Fire();
    7.     }
    8.   }
    9. }
    Then in the subclasses you just override it the same way:
    Code (CSharp):
    1. public class Bazooka : Weapon {
    2.   public override void Fire() {
    3.     // Do bazooka-specific stuff here.
    4.   }
    5. }
     
    Last edited: Sep 24, 2020
    Vryken likes this.
  3. Gokushivum

    Gokushivum

    Joined:
    Oct 21, 2014
    Posts:
    7
    So I would override the fire method, but wouldn't I still need to call the specific weapon class instead of the superclass?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Nope. If the weapon reference you have is to an instance of the child class, then the override method will take over.
     
  5. Gokushivum

    Gokushivum

    Joined:
    Oct 21, 2014
    Posts:
    7
    Thank you, that worked! I need to read up a little more on virtual methods so I can understand that better.