GetComponent and inheritance.

Hi there Uniteers, I am in need of your help:

  • I have a script called “QuestClass”.
  • Several other scripts is derived from QuestClass, these are named Q01, Q02, Q03…
  • Q01, Q02, Q03 is applied on a GameObject we call “QuestGiver”.

Okay, so in a secondary script, I want to reach these three Q-Scripts by using.

QuestClass[] tempArr = QuestGiver.GetComponents<QuestClass>();
foreach(QuestClass i in tempArr)
{
Debug.Log(i.GetType().toString();
}

What I get in return is:
Q1, Q1, Q1…

But what I expected was:
Q1, Q2, Q3…

The funny thing is, it recognizes at least how many scripts there is, so if there is five Q-scripts such as:
Q2, Q5, Q7, Q1, Q7…

It prints:
Q2, Q2, Q2, Q2, Q2

Is it just me who are missing something fundamentally about C# here or Unity? Perhaps I am doing it right, but the problem lies elsewhere? Feel free to ask questions, if I am not specific enough or haven’t made my self clear.

That’s odd, not what I would expect either. What happens if you try:

Component[] tempArr = QuestGiver.GetComponents(typeof(QuestClass));
Component[] tempArr = QuestGiver.GetComponents(typeof(QuestClass));

 foreach (Component i in tempArr )
{
       Debug.Log(i.GetComponent<QuestClass>().GetType().ToString());
}

Provides the same result.

The attached scripts are Q05, Q06, Q07, Q08

But what I get is Q05, Q05, Q05, Q05

Okay, this is damn strange. I have done absolutely nothing, and yet it appeared to fix itself with the following code:

QuestClass[] tempQuestList = hitObject.GetComponents<QuestClass>();
                       foreach (QuestClass i in tempQuestList)
                       {
                           i.OnActivate();
                       }