- Home /
The question is answered
Convert Component to Actual Type C# to call a coroutine
So basically I need to convert a generic component to a specific one in order to call a coroutine. allComponentsList is a list that contains any or all of three different script types located on the gameObject. Dialogue, Choice Dialogue, and JumpTo. The gameobject contains multiple of the same type of each script or may not contain any of a certain type. (like 3 Dialogue scripts, 2 choice scripts and maybe even 0 JumpTo scripts). I can't just use three different lists for that because the order of the lists matter because that is the order they will execute in so I have to use a generic Component List. My iff statements work. Here is what I was hoping it would be something like this (has errors). counter is an int variable that keeps track of what item it is at in the list of the 3 script options
if (allComponentsList[counter].ToString() == gameObject.name + " (Dialogue)")
{
Debug.Log("Next one is a dialogue");
//I am trying to call the setDialogue coroutine from a script but I get errors because allComponentsList is just a generic Component and not the actual Dialogue script. Invoke doesn't work for coroutines either
StartCoroutine(allComponentsList[counter].setDialogue());
//and it also can't convert methods to iEnumerators (this is just another attempts to get it to work. I only need to call it once):
StartCoroutine(allComponentsList[counter].GetType().GetMethod("setDialogue"));
counter += 1;
}
else if (allComponentsList[counter].ToString() == gameObject.name + " (ChoiceDialogue)")
{
//same thing here but this script has a parameter
StartCoroutine(allComponentsList[counter].setDialogue(gameObject));
counter += 1;
}
else if (allComponentsList[counter].ToString() == gameObject.name + " (JumpTo)")
{
//same thing here but different coroutine name
StartCoroutine(allComponentsList[counter].jumpToScript());
counter += 1;
}
since you're Not just interested in the actual type but want to do something with is, you can try this:
Dialogue v1 = allComponentsList[counter] as Dialogue;
if (v1 != null)
{
v1.StartCoroutine...
}
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Coroutines and Invoke not working 1 Answer
How does Invoke() work? 1 Answer
Dynamic function calling 2 Answers
In a tile-based game, best way to select multiple tiles via dragging? 1 Answer