Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

weapon classes and using a gameobject for a projectile

Discussion in 'Scripting' started by poopipe, Mar 31, 2016.

  1. poopipe

    poopipe

    Joined:
    Apr 13, 2014
    Posts:
    2
    Hello chaps,

    I'm sure this has been answered in abstract many times but I am a terrible programmer who can only learn by example.

    excuse the misuse of terminology in the following..

    I'm trying to implement a weapon class with a few basic methods - specifically a fire() method and I want to be able to specifiy a gameObject as a projectile in classes that inherit from the base weapon class


    I've set myself up fine with the base class (Weapon) that includes a Fire() method and a class that inherits from it (inheritedGun) which can implement and override the behaviour of the Fire() method

    my problem is that I don't know how to define a projectile at the inheriteWeapon level and cause the base fire function to shoot it (or tell me it's name)


    if someone could explain why I am failing at this I would be very grateful

    thanks..

    here is code:

    base class
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Weapon : MonoBehaviour {
    6.  
    7.     //testy debug crap - bin it later
    8.     protected string type = "weapon";
    9.  
    10.     protected virtual string getType()
    11.     {
    12.         Debug.Log("Weapon::getType() called");
    13.         return type;
    14.     }
    15.     //-----------------------------------
    16.  
    17.     public int Damage;
    18.     public float Spread;
    19.     public int Capacity;
    20.     public GameObject projectilePrefab;
    21.  
    22.     public Weapon(int dmg, float spr, int cap)
    23.     {
    24.         Damage = dmg;
    25.         Spread = spr;
    26.         Capacity = cap;
    27.     }
    28.  
    29.     public Weapon()
    30.     {
    31.         Damage = 10;
    32.         Spread = 0.2f;
    33.         Capacity = 100;
    34.     }
    35.  
    36.  
    37.  
    38.     public virtual void Fire()
    39.     {
    40.  
    41.         Debug.Log();
    42.         Debug.Log("Weapon::Fire() called");
    43.       //  return string;
    44.     }
    45.  
    46. }
    47.  

    inherited weapon

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class inheritedGun : Weapon {
    6.  
    7.     //i want to send this up to the Weapon class so i  can shoot it using the base class Fire() method
    8.     public GameObject projectilePrefab;
    9.  
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         type = "tester";
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         if (Input.GetKeyDown(KeyCode.Space))
    20.         {
    21.             Fire();
    22.         }
    23.     }
    24.  
    25.     protected string getType()
    26.     {
    27.         Debug.Log("Tester::getType() called");
    28.         return type;
    29.     }
    30.  
    31.     public override void Fire()
    32.     {
    33.        // I dont seem to be able to pass a gameObject as a parameter to the base.Fire() method
    34.         base.Fire();
    35.         Debug.Log("overrided Weapon::Fire() called");
    36.     }
    37.  
    38. }
    39.  
    40.  
     
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    I'm not sure if I've misunderstood your question, but you shouldn't need to define the projectilePrefab in both the parent class and the child class - it misses the whole point of inheritance. The overridden Fire method doesn't need to 'pass in' the projectilePrefab - its already defined by the parent.

    Remove the 'public GameObject projectilePrefab' from the inherited class, and in the virtual Fire() method, add in

    Code (csharp):
    1. Debug.Log("Weapon name is " +projectilePrefab.name");
    And you can see that the base method will name whatever the assigned projectilePrefab is - no matter if you are calling from the base class or from an inherited class.
     
    LeftyRighty likes this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    base.Fire() doesn't have any parameters...


    edit:
    going back to basic c# inheritence
    https://msdn.microsoft.com/en-us/library/ms173149(v=vs.100).aspx

    look at the example, in the base class ID and a few other properties are declared. In the inherited class it just instantiates the property (no additional declaration).
     
  4. poopipe

    poopipe

    Joined:
    Apr 13, 2014
    Posts:
    2
    Thanks chaps,

    it was indeed me missing the point of inheritance (partly due to having had a couple of glasses of gin )
    I've done as Zaladur suggests and it works fine.