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
0
Question by ExtinctSpecie · Mar 18, 2017 at 08:25 PM · componentinheritance

get component of gameobject that is type of base class

hello, I have 5 scripts basically

1)Tower

2)Tower_Damage : Tower

3)TurrentLevelOne: Tower_Damage

4)Tower_Buffer:Tower

5)AttackSpeedBuff : Tower_Buffer

and then I have a shop where I can buy these towers TurrentLevelOnePrefab and AttackSpeedBuffPrefab

and i want to have access to the AttackSpeedBuff script or the TurrentLevelOne script but i dont know which script is attached to the bought tower

something like GetComponent(typeof(Tower)) but this one needs to be cast to (TurrentLevelOne) for example so i can have access to its methods , any ideas?

thanks in advance

Comment
Add comment · 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 ExtinctSpecie · Mar 18, 2017 at 08:35 PM 0
Share

i can do this with interfaces i think but i wanted to do it with abstract classes

1 Reply

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

Answer by Commoble · Mar 18, 2017 at 08:47 PM

There's a bunch of ways to do this (assuming Tower is a MonoBehaviour) . Here's a few:


Here's a bad way to do it (to show you you can use GetComponent on subclasses):

 AttackSpeedBuff speedBuffComponent = towerPrefab.GetComponent<AttackSpeedBuff>();
 
 if (speedBuffComponent != null)
 {
     // do stuff
 }
 
 TurretLevelOne turretComponent = towerPrefab.GetComponent<AttackSpeedBuff>();
 
 if (turretComponent != null)
 {
     // do stuff
 }



Here's another bad way to do it (to show you how to use typeof and GetType to determine what type a component is):

     Tower towerComponent = towerPrefab.GetComponent<Tower>();
     
     if (towerComponent != null)
     {
         // this is true if the tower component on the prefab is an
         // AttackSpeedBuff component or a subclass of AttackSpeedBuff
         if (typeof(AttackSpeedBuff).IsAssignableFrom(towerComponent.GetType()))
         {
         AttackSpeedBuff asbuff = (AttackSpeedBuff)towerComponent;
             // do stuff
         }
         else if (typeof(TurretLevelOne).IsAssignableFrom(towerComponent.GetType()))
         {
         TurretLevelOne tl1 = (TurretLevel1)towerComponent;
             // do stuff
         }
     }



Both of these ways work. They're not good practice, but they'll do what you need with minimal code reworking. A better way to do it would be to rework your classes so you can get them to do what you need them to do without your shop script knowing what specific kind of tower they are.

When I'm in this situation, I always try to refactor my classes before I resort to things like the solutions I gave above. Sometimes it takes a little creative thinking, but it's usually doable.

Comment
Add comment · Show 7 · 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 ExtinctSpecie · Mar 18, 2017 at 09:02 PM 0
Share
 this is what i wanted to avoid actually xD
 but i managed to do it by adding a simple interface
 
 public interface ITower 
 
 {
     bool BuyTower();
 }
 
 and now my Tower has this added
 
 public abstract class Tower  : $$anonymous$$onobehaviour , ITower
 {
        #region ITower implementation
 
     public abstract bool BuyTower ();
 
     #endregion
 }
 
 
avatar image Commoble ExtinctSpecie · Mar 18, 2017 at 09:12 PM 1
Share

@ExtinctSpecie Just a heads up, if your Towers are the only thing that implement ITower, there's no need to make an interface; you can define your abstract and virtual functions in the Tower base class and have your subclasses override the functions, e.g.

 public abstract class Tower : $$anonymous$$onoBehaviour
 {
     public abstract bool BuyTower();
 }
 
 public class AttackSpeedBuff : Tower
 {
     public override bool BuyTower()
     {
         // define the function
     }
 }

Interfaces are only necessary when you need completely unrelated things to have the same function.

avatar image ExtinctSpecie · Mar 18, 2017 at 09:05 PM 0
Share

but thanks for your time anyway that is an answer even tho not the best because when you get many towers you get lost into if statements and bad code

avatar image RobAnthem ExtinctSpecie · Mar 18, 2017 at 09:28 PM 0
Share

You may also consider creating base classes for your turrets and buffs, thus allowing you to call the turret or buff universally, not matter what level it is. Also, I'm not sure why your Tower_Damage derives from Tower. Also if your buffs aren't drastically different, they could all be one classes reworked through the inspector on each instance. Just remember the OOP principles.

avatar image ExtinctSpecie RobAnthem · Mar 19, 2017 at 04:48 PM 0
Share

Tower describes my stuff that every tower has like a range for example or cost Tower_Damage describes what a tower that can deal damage can do for example HitTarget(GameObject target) but my buff towers do not need something like hit target or rotate or findclosest target this way i can limit and not write code or variables that are never going to be used

i dont know maybe im doing it wrong but thats the best thing i can come up with at the moment

Show more comments

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

65 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 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

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

Inheritance and Component Types in C# 2 Answers

Inheritance vs RequireComponent -1 Answers

Script component in Editor vs. runtime-dependent inheritance 0 Answers

How unity works under the hood? 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