- Home /
Choosing a SubClass inherited from parent class in hierarchy !
i created weapon class which is the parent class,and it has 2 childs "gun subclass" and "MachinGun sub class" my aim,is how to switch betweel class ,have i to use list and displaying in UI ? or i workd IEnumerator to switch between them ? am kind stuck here rly`here is my code`
using UnityEngine;
using System.Collections;
public class Inherit : MonoBehaviour {
public int Damage;
public int Ammo;
public int health;
public string[] _NameWep;
public float _TimeBetwenShots;
public int _BulletSpeed;
public float _ShotCounter;
public GameObject Bullet_G;
//public GameObject _BulletF;
public Transform _FirePoint;
//public int numSelectors = 5;
void Update()
{
Firing ();
}
public virtual void Firing()
{ //Bullet_G = new GameObject[numSelectors];
if (Input.GetButton ("Fire1"))
{
_ShotCounter -= Time.deltaTime;
if (_ShotCounter <= 0) {
_ShotCounter = _TimeBetwenShots;
//for(int i = 0; i < numSelectors; i++)
//{
GameObject bulletObj = Instantiate (Bullet_G, _FirePoint.position, _FirePoint.rotation) as GameObject;
//Bullet_G [numSelectors] = bulletObj;
bulletObj.GetComponent<Rigidbody2D> ().velocity = transform.right * _BulletSpeed;
} else
{_ShotCounter = 0;
}
}
}
public class gun : Inherit
{
void Update()
{
Firing(); // i called Firing function in gun subclass to shoot bullets
}
}
public class MachinGun : Inherit
{
void Start () {
}
// Update is called once per frame
void Update () {
}
}
}
Answer by Blue-Cut · Sep 09, 2016 at 02:15 PM
Hello,
"Switch between class" doesn't mean anything. A class is a what your object is, and your object does't "change its class".
If I understand your question, what you need to do is creating 2 objects : 1 Gun and 1 MachinGun, and your character is able to switch between those two weapons.
The concept is very different : your object does not change, but you have 2 objects, and only one of them is used by the player.
So in the script that control your player, you can have a property Gun and a property MachinGun, then you display those two weapons in your UI so the player can choose which one to use.
Does it help ?
but i keep using inheritance ? and i call subclass in player control !? that's it ?
Yes that's it. Your inheritance is ok, and your player controller knows 2 objects that have the subclasses types. The inheritance is usefull if your 2 subclasses share some functions. Tell me if you need more detail about why using inheritance.
Since i want always learn new things,so feel free to give me more informations about inheritance,i know that it helps to creat strong relation between class,and its optimal ! but it would be nice if you give me new tips about inheritance .
This is pretty easy. Basically, a subclass is a class that has the same properties than the parent, but it has something more, and because of this "something more", you want to change a bit the behaviour of the object.
I am going to give you an example with your context : weapons. Let's say that you have a basic Weapon with the standard behaviour of a weapon, and then you create subclasses with modified behaviours.
This is the basic weapon :
public class Weapon : $$anonymous$$onoBehaviour
{
/**
* A weapon make some damage to a target
*/
public int damage;
/**
* And let's say that a specific weapon shot a specific bullet
* So you affect a prefab to the weapon
*/
public Transform ammunitionType;
/**
* And then let's say you have a bullet maker that creates a bullet
* with a specific prefab and the damage data
*/
public Bullet$$anonymous$$aker bullet$$anonymous$$aker;
/**
* OnFire asks the bullet maker to create a bullet
* with the prefab and the damage you set to the weapon
* "virtual" means you can override this function in a subclass
*/
public virtual void OnFire()
{
bullet$$anonymous$$aker.Shoot(damage, amunitionType);
}
}
Then you create a Gun class, that is a weapon, but with something more :
public class Gun : Weapon
{
/**
* Now, the gun is a weapon that have a limited number of ammunitions
*/
public int nbBullet;
/**
* Let's rewrite the OnFire function with "override" keyword
*/
public override void OnFire()
{
// With a gun, I can shot only if I still have bullets
if(nbBullet > 0)
{
/**
* If I can shoot, I want to do the same thing that in the parent class
* So I call "base"
* Note that as your gun is a subclass, it knows all the public/protected
* properies of the parent, so it has damage/ammunitionType/bullet$$anonymous$$aker
*/
base.OnFire();
// Then as I shot, I remove 1 bullet
nbBullet --;
}
}
}
Then when you tell in your code that an object is a "Weapon", it can be a Weapon or a Weapon subclass. And if its a subclass, the subclass methods are called : public class Player : $$anonymous$$onoBehaviour { /** Your player has some weapons In this array, you can put any object related to the weapon class So you can put Weapon and Gun / public Weapon[] weapons;
/**
* Only one weapon is selected
* The selected weapon is one of these in your array
*/
private Weapon _selected;
/**
* When the player use the weapon, we call the OnFire of this weapon
* If the weapon is a Gun, so the method that will be call is the
* OnFire of THE GUN class and NOT THE WEAPON class
*/
public void Fire()
{
_selected.OnFire();
}
}
I am trying to correct the formatting problem in my post since the last 10 $$anonymous$$utes but the submit button doesn't submit anything, maybe because of my current low connexion ? :s
Thank you for giving me brillant ideas,now i'am trying to put my weapons in an array or list,and displaying them in UI,once the gamer select the weapon,it will appear in the game !i will start doing it now
Answer by tqkiettk10 · Sep 09, 2016 at 02:11 PM
you can attach MachinGun script to MachinGun Gameobject, attach gun script to Gun Gameobject. Put them to your player game object and set active MachinGun to false. When Player want to change weapon from gun to MachinGun (with UI button or special Keycode) you just need disable Gun gameobject and enable MachinGun object.
i will try to implement this logic ,but i have to include arrays or lists ? to manage weapons choice ?
why you don't manage in PlayerController and in Inherit Script change public string[] _NameWep; to public string _nameWep;?