• 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
Question by TheNinjassin · Jan 08, 2016 at 01:06 PM · instantiateinheritancewaitforsecondsmonobehaviourabstract

Abstract Class - Monobehaviour Functions

So I have a bit of a dilemma.

I want to use an inheritance structure for guns in my game. I want to make guns an abstract class and then have types of guns (lasers, rifles, shotguns) which inherit from it and then create specific instances of these weapons. At the moment I'm doing this as an abstract class because I want to be able to create instances of guns and add them to a list. I don't want them to exist as external MonoBehaviour scripts because I want to be able to have a list of weapons in the players inventory / storage and don't want an instance of each MonoBehaviour on the GameObject. I am doing so thusly:

 ActiveWeapon = new Rifle("Starter Gun", 30, 5, 0.3f, 2.0f, 100.0f, true, 3.0f);

The issue I'm encountering is that I want to be able to utilise some of the methods which are derivative of MonoBehaviours for example :

 public override void Shoot ()
     {
         if (CanShoot) 
         {
                     StartCoroutine(Cooldown());
             Rigidbody bullet = Rigidbody.Instantiate(BulletPrefab, transform.position, transform.rotation);
             ... // Addforce etc.
             ...
         }
     }
 public IEnumerator Cooldown()
     {
         CanShoot = false;
         yield return new WaitForSeconds (FireRate);
         CanShoot = true;
     }

Obviously since this isn't a MonoBehaviour class I can't use functions such as Instantiate to create bullets or WaitForSeconds to stagger them.

The issue is that most of these functions and references are specific to MonoBehaviours. However my guns will be attached to a MonoBehaviour (PlayerResources.cs). How should I tackle this issue?

Comment
andrew-lukasik

People who like this

1 Show 1
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 andrew-lukasik · Jan 24, 2016 at 11:06 PM 0
Share

Hi. Imho PlayerResources could for example expose static Instantiate wrapper method PlayerResources.Instantiate(prefab,position,rotation). Not even to mention that bullet should be handled by some kind of pooling manager and not Object.Instantiate. Also you can think about replacing Cooldown method with, like, canShoot property maybe:

 private float lastShotTime = 0f;
 private float cooldownTime = 1f;
 public bool canShoot {get{return Time.time>lastShotTime+cooldownTime ? true : false;}}

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

40 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

Related Questions

Can I call a class's method which inherit monobehaviour by a normal class? 0 Answers

Instantiate without inheriting from MonoBehaviour 2 Answers

How do I make a game object appear a few seconds into the game? 1 Answer

C# Extending Script causes InvalidCastException on Instantiate 1 Answer

Behind the scenes voodoo w/ Instantiate 2 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