Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Agent654 · Mar 11, 2015 at 02:45 PM · variablegetcomponentruntimecomponentsubclass

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?

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
0

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!

Comment
Add comment · 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

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

21 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

Related Questions

How to access a variable for a Component of a different GameObject ? 1 Answer

Retrieving componants and comparing with another 2 Answers

Checking if a component has been removed or not at runtime 1 Answer

Change the Halo Component (not the default) in a light in C#??? 0 Answers

Is it possible to set variables for classes? 2 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