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 Crump · May 11, 2012 at 01:08 AM · triggersinteractioninterfaces

GetComponent and Interfaces and warnings

So I have this IInteractable that I tag some of my monobehaviours with. The player has a trigger box attached to the front of them, and I have this little method running.

 void OnTriggerEnter(Collider _col)
 {
     if (_col.GetComponent(typeof(IInteractable)))
     {
         interactObject = _col.GetComponent(typeof(IInteractable)) as IInteractable;      
     }
 }

and then later in an update I call :

 interactObject.Action()

to get the whole thing working.

Anyway, I'm having a problem, more of an annoyance really. Every time the trigger box touches anything in the game, I get this warning:

GetComponent requires that the requested component 'IInteractable' is inherited or implemented by 'objectName'.

And literally everything the trigger box touches gives this warning. The floor, the walls, the player themselves. This makes the console rather bothersome to use, even if I turn collapse on. Anyone have any ideas on how to make it not generate hundreds of warnings?

I will say, I found a solution that works, but requires a little more set up than I'd like. Just putting the box on the player on it's own layer, say PlayerInteract, and then every object that you can interact with in the game on it's own layer, Interactable. And then making it so PlayerInteract touches no layers except Interactable. This works, but I'm trying to see if I can just change the if statement to something else so I can avoid having to check layers all the time.

Comment
Add comment · Show 2
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 syclamoth · May 11, 2012 at 01:26 AM 3
Share

Basically, GetComponent won't work the way you want it to. It looks for, specifically, the type IInteractable- it won't detect a component that simply obeys the interface. Because IInteractable is an interface, not a type, GetComponent won't work with it.

avatar image Kryptos · May 11, 2012 at 08:02 AM 0
Share

@syclamoth you are mistaking. It will work as long as the object does inherits Component. I use it a lot in my projects.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Crump · May 11, 2012 at 05:56 AM

So it does work. It just throws a lot of warnings. Like a lot a lot. I'm just trying to see if I can get the warnings to go away. It expects that everything it touches to have implemented IInteractable in some shape, way or form. What's odd, to me at least, is that if it's not an interface, it doesn't throw that warning. Unity is totally cool with doing a GetComponent<> as a check, and failing without throwing a warning. Interfaces still work, but they seem to behave differently.

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 Kryptos · May 11, 2012 at 08:06 AM 0
Share

GetComponent<> performs a check to see if the object does inherits Component.

On the other hand, GetComponent(typeof) does not. But your object still needs to inherits Component (or $$anonymous$$onoBehaviour) somehow.

This is indeed very useful for interfaces. You can ignore the warning safely: the method will return null if your object is not a component.

avatar image
0

Answer by Pangamini · May 17, 2012 at 05:06 PM

I had the same problem. The GetComponent works well, but throws warnings. Here's my solution a custom GetComponent method:

 def GetComponent[of T(class)](gameobj as GameObject) as T:
         comps = gameobj.GetComponents(Component)
         for comp in comps:
             if comp as T != null:
                 return comp as T
         return null
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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

hotspots in 3d space 0 Answers

How can a game object detect that it is being hit by a raycast? 3 Answers

Getting only one collider form OnTriggerEnter2D or OnCollisionEnter2D. 0 Answers

Trigger with get key down problems 0 Answers

OnTriggerEnter being called event though there is no collision? 0 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