Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Silerrain · Sep 09, 2016 at 10:39 AM · inheritancehierarchyswitchweaponsguns

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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 ?

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Silerrain · Sep 09, 2016 at 03:45 PM 0
Share

but i keep using inheritance ? and i call subclass in player control !? that's it ?

avatar image Blue-Cut Silerrain · Sep 09, 2016 at 03:56 PM 0
Share

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.

avatar image Silerrain Blue-Cut · Sep 09, 2016 at 04:07 PM 0
Share

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 .

avatar image Blue-Cut · Sep 10, 2016 at 08:46 AM 1
Share

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();
 }

}

avatar image Blue-Cut Blue-Cut · Sep 10, 2016 at 09:12 AM 0
Share

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

avatar image Silerrain Blue-Cut · Sep 10, 2016 at 10:59 AM 0
Share

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

avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Silerrain · Sep 09, 2016 at 03:48 PM 0
Share

i will try to implement this logic ,but i have to include arrays or lists ? to manage weapons choice ?

avatar image tqkiettk10 Silerrain · Sep 09, 2016 at 03:57 PM 0
Share

why you don't manage in PlayerController and in Inherit Script change public string[] _NameWep; to public string _nameWep;?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

My automatic gun don't work help! 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Help needed in changing weapons 2 Answers

Is protected variables protected in hierarchy ? 2 Answers

FPS Weapon inventory script help 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges