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 rlyhere 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 () {

		}

	}


}

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 ?

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.