• 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
4
Question by MDarkwing · Aug 05, 2014 at 01:47 PM · getcomponent

Getting derived class using base class in GetComponent

I think this is best explainalbe by example:

Let's make base class be

 public abstract class Ability : Monobehaviour
 {
      public Transform Caster;
 
 }

I have a unit which has few abilities that are different but all the abilities, need caster reference so it makes sense to make abstract class and make ability classes derive from it.

Now if I have Game Object for each ability with responding derived class on it say: FireballSpell LightningSpell, EarthShield, (classes that have ability as its base and are each on separate game object) can I if I have list of this objects get responding spell classes like this:

 foreach(GameObject go in Spells) //Spells is List<GameObjects>
 {
     go.GetComponent<Ability>().Caster = Caster;
 }

where Caster is predefined GameObject and List Spells is filled with FireballSpell, LightningSpell and EarthShield.

Thank you in advance

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
14
Best Answer Wiki

Answer by JoshPeterson · Aug 05, 2014 at 03:44 PM

Yes, this will work as I believe you expect it to. I've defined a few scripts:

Ability.cs:

 using UnityEngine;
 using System.Collections;
 
 public abstract class Ability : MonoBehaviour {
     public Transform Caster;
 
     private bool positionDisplayed = false;
 
     protected void DisplayCasterPosition(string spellName) {
         if (!positionDisplayed) {
             Debug.Log(string.Format("{0} {1}", spellName, GetCasterPosition()));
             positionDisplayed = true;
         }
     }
 
     private string GetCasterPosition() {
         return string.Format("Caster position: {0},{1},{2}",
                              Caster.position.x, Caster.position.y,
                              Caster.position.z);
     }
 }

EasrtShieldSpell.cs:

 using UnityEngine;
 using System.Collections;
 
 public class EarthShieldSpell : Ability {
     void Update () {
         DisplayCasterPosition ("EarthShieldSpell");
     }
 }

FireBallSpell.cs:

 using UnityEngine;
 using System.Collections;
 
 public class FireBallSpell : Ability {
     void Update () {
         DisplayCasterPosition ("FireBallSpell");
     }
 }

LightningSpell.cs:

 using UnityEngine;
 using System.Collections;
 
 public class LightningSpell : Ability {
     void Update () {
         DisplayCasterPosition ("LightningSpell");
     }
 }

ListOfSpells.cs

 using System.Collections.Generic;
 using UnityEngine;
 
 public class ListOfSpells : MonoBehaviour {
     public List<GameObject> spells;
 
     void Start () {
         foreach (GameObject go in spells) {
             go.GetComponent<Ability>().Caster = this.transform;
         }
     }
 }

Then I've created one game object for each of the three spells, and added one spell as a component to each game object. I've also created a game object that uses the ListOfSpells.cs script, and assigned each of the spell game objects to the spells list:

alt text

Now I see the following console output:

alt text

If you change the Position X, Y, or Z values on the Tranform for the ListOfSpellsGameObject and play again, the values logged for each spell should match.


listofspellsgameobjectscreenshot.png (21.2 kB)
consoleoutputscreenshot.png (12.7 kB)
Comment
Add comment · Show 1 · 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 MDarkwing · Aug 06, 2014 at 10:53 AM 1
Share

Thanks dude, I've managed to do it myself $$anonymous$$utes after posting question, but you've made quite an answer, I thought of deleting the question, but with answer this complete it would be shame to do so. Cheers!

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

22 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

Related Questions

Passing a string from Javascript to C# 1 Answer

How to Remove Multiple Components of Same Type from Game Object? 2 Answers

AddComponent not working 3 Answers

GetComponent Errors 2 Answers

Updating GUIText 3 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