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 /
  • Help Room /
avatar image
0
Question by TheNinjassin · Dec 29, 2015 at 11:55 AM · c#randomabstract

Instantiation Of Abstract Classes

Hi Guys,

I'm a little confused on how to create instances based on my abstract class. My Inheritance structure looks like this:

Gun > Rifle > Specific Weapon

I've declared the abstract classes in a specific C# script (below). I want to be able to create specific guns at runtime with randomised statistics and then attach them to an array in my players inventory. I just wanted to know what the syntax for creating a Specific Weapon would be from inside a method for example a loot method.

Gun Class

 public abstract class Gun
 {
     public string     Name;
     public int         Strength;
     public float     DistanceFallOff;
     public bool     CanScope;
     //public GameObject gunModel;
 
     public abstract void Shoot();
 }

Rifle Class

 public abstract class Rifle : Gun
 {
     public int        MaxAmmo;
     public int         CurrentAmmo;
     public float     FireRate;
     public float     ReloadRate;
 
     public Rifle(string name, int maxAmmo, int strength, float fireRate, float reloadRate, bool canScope)
     {
         Name = name;
         MaxAmmo = maxAmmo;
         Strength = strength;
         FireRate = fireRate;
         ReloadRate = reloadRate;
         CanScope = canScope;
         CurrentAmmo = maxAmmo;
     }
 
     public IEnumerator Reload()
     {
         yield return new WaitForSeconds(ReloadRate);
         CurrentAmmo = MaxAmmo;
     }
 }

 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by A_Li_N · Dec 30, 2015 at 05:18 AM

The point of abstract classes is to give all derived classes the same starting point; So your first level is correct:

Gun is an abstract concept; it contains basic information that is the same for all types of guns; rifles, pistols, etc.

However, your Rifle being abstract is not overly correct, unless you never want an actual 'Rifle' instance.

Your stated structure says 'Specific Weapon', but I think you defined that too deep and are actually attempting to mean 'Rifle' has multiple instances. In this situation, you do not want Rifle to be abstract as you actually want instances of them to be created. The only reason to have Rifle be abstract is if you are always planning on instantiating derived classes (ie public class MySuperRifle : Rifle, public class TheLimpRifle : Rifle)

Changing Rifle from abstract doesn't mean it can't be anonymous or random; it just means you can create a Rifle directly, which is what your own answer shows you are wanting to do. It also doesn't inhibit you from inheriting further, meaning you can still do public class MyUniqueRifle : Rifle, but you can also do Rifle r = new Rifle(...);

Comment
Add comment · Show 3 · 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 TheNinjassin · Dec 30, 2015 at 02:55 PM 0
Share

That is so appreciated! Thank you for clarifying this for me, yes I was clearly confused partly about the issue and partly in terms of my structure. I do have one more question though, since my abstract class isn't a monobehaviour, how should I go about creating a method to reload. I instinctively want to create a coroutine and use WaitForSeconds to toggle a boolean. I don't really know what the alternative to this is.

avatar image A_Li_N TheNinjassin · Dec 31, 2015 at 01:49 AM 0
Share

Reload as in reload the weapon's ammo?

Since all Guns reload, I would put a public void Reload() method in the Guns class. This method should do exactly what it says it's going to, which is reload the gun. If the gun has a delay between the start and finish of the Reload, yes you could use WaitForSeconds before actually adding the ammo capacity. If there is an animation, the method could play it, etc, etc.

Then, in the Update method of your input handling class (which would be a $$anonymous$$onoBehaviour), you would check if the reload button was pressed and reload the current gun, ie: currentGun.Reload();

(I'll leave it to you to handle finding/tracking reference to the 'currentGun')

avatar image A_Li_N A_Li_N · Dec 31, 2015 at 01:50 AM 0
Share

To further this, you could mark the Reload function as virtual, which would allow 'Rifle' (or any other derived class) to override the behavior if it is different (plays a different animation/sound, etc).

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

50 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with specifics in Random.Range 1 Answer

Spawn the next room by opening a door. 3 Answers

How to make RANDOM COLOR on shader 1 Answer

Instantiate GameOBject with changing size in relation to another GameObject in the scene C# 0 Answers

Randonly Losing reference to GameObject from prefab 1 Answer


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