- Home /
How to compare variable component types between GameObjects at run time?
Greetings all;
I recently ran into a slight "problem" in one of my projects that I was unable to easily find resolution for, so I figured I'd propose the question and the answer I was able to come up with in hopes that I could save someone else an hour or so picking apart other answers in an effort to build a workable solution. Bear with me if I'm stating the obvious, but a post like this would've immediately saved me time.
Arbitrary Disclaimer:
I am in no way a coding expert, but my provided answer was the most eloquent solution that I was able to come up with. If you have a better way to handle this scenario, please feel free to comment and provide solutions as you see fit!
The Problem:
I needed to compare two GameObjects (Object "Alpha" and object "Beta") to see if a subclass component (in this case, a script) were attached to both objects. However, the Component type was variable, as there are multiple scripts to test for, taking different actions based on that type and whether-or-not both GameObjects contained the script.
The "obvious" answer was to simply write IF statements to handle each of the specific component types, for example:
IF(objectAlpha.GetComponent<type1>())...
IF(objectAlpha.GetComponent<type2>())...
...because of the fact that in order to use GetComponent, I would have to know the type I was looking for. But this approach was going to turn into a maintenance nightmare as more Component subclass types are added (future thinking).
So I wanted to build a function that would accept a GameObject and a Component as parameters, so that I could test for both the instance of the GameObject (for duplication testing) as well as the aforementioned Component type - that if present would trigger a different result.
The Question
How do I test for GetComponent type matching at run time to take different actions?
Answer by Agent654 · Mar 11, 2015 at 03:00 PM
My Solution
Essentially what I ended up doing to solve this issue, was to create a function that takes a GameObject and a Component as a parameter, to compare against an array of GameObjects to find out if the Subclass component was already present in any of the GameObjects of the array tracking them.
public bool IsThereaDupe(GameObject testingObject, Component testingType)
{
if(trackedObjects.Count > 0)
{
foreach(GameObject individualObject in trackedObjects)
{
//Store all of the components of the tracked (array) object, to check against the type that was passed
Component[] objectComponents = individualObject.GetComponents(typeof (Component));
if(individualObject == testingObject)
{
//You found the duplicate instance of the GameObject, don't count as a true comparison
return false;
}
else
{
//It's not the instance, so check for Component type matching
foreach(Component individualComponent in objectComponents)
{
if(testingType.GetType() == individualComponent.GetType()) //The actual subclass comparison
{
//Could tag the array index of this matched object here, for reference later
return true;
}
}
}
}
return false;
}
else
{
return false;
}
}
By doing this, now I can simply perform a function call from wherever I want to know this comparison to then branch the logic accordingly, taking path A if there is no match, or path B if there is.
So my call would be something like so:
if(IsThereaDupe(objectAlpha, attachedComponent)...
... where "attachedComponent" is the specific component I want for comparison. In my project, it's a particular subclass script.
Like I said, it might not be the most graceful way to handle this sort of situation, but this did exactly what I needed it to, and if it could save anyone an hour or two cobbling a solution together from other posts, then I've done my due diligence.
If you found this helpful at all, please upvote - thanks!