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 ikelaiah · Sep 22, 2014 at 06:26 AM · c#interfacefindobjectoftype

How do I get gameobjects to fire a method based on an interface?

Following the interface tutorial from here. There are a number of gameobjects in the scene that implements IDamageable. How do I fire all void Damage(float damage) belonging to gameobjects that implement IDamageable?

I tried the following, but Unity says "Type has to be derived from UnityEngine.Object."

 var damageableObjects = GameObject.FindObjectsOfType(typeof(IDamageable)) as IDamageable[];
 
 foreach(IDamageable d in damageableObjects)
    d.Damage(1.0f);



Comment
Add comment · Show 3
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 Freaking-Pingo · Sep 22, 2014 at 06:50 AM 0
Share

This is just a wild guess, because I haven't worked with Javascript. The tutorial is using C#, but in your code example above you are using javascript, so ins$$anonymous$$d use a c# script and make use of:

 IDamageable[] damageableObjects = GameObject.FindObjectsOfType(typeof(IDamageable)) as IDamageable[];
  
 foreach(IDamageable d in damageableObjects)
    d.Damage(1.0f);
avatar image dmg0600 · Sep 22, 2014 at 07:52 AM 0
Share

One can also use var in C#, the only thing wrong is the separated foreach statement.

avatar image ikelaiah · Sep 22, 2014 at 09:29 AM 0
Share

@dmg0600 thanks for picking that up for each, ins$$anonymous$$d of foreach.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by dmg0600 · Sep 22, 2014 at 07:52 AM

Take a look at this: Link to forums

Or just make it work through inheritance instead of interfaces. It depends if you are going to use a lot of interfaces or just IDamagable, as the workaround showed in that post is not very efficiency-friendly.

Comment
Add comment · Show 5 · 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 ikelaiah · Sep 22, 2014 at 09:36 AM 0
Share

Thanks for the link. Why is the proposed solution not efficient friendly? LINQ?

avatar image dmg0600 · Sep 22, 2014 at 09:43 AM 0
Share

Not because of Linq but because it goes through all the components in the GameObject searching for all of them.

Since it is just for one GameObject there is no problem, but be careful if you want to do this for a lot of objects.

avatar image ikelaiah · Sep 22, 2014 at 09:48 AM 0
Share

I see, menuObject.GetComponents(); goes through all the components in the gameobject. I've modified my code to use inheritance rather than using interfaces.

avatar image glantucan · Sep 23, 2014 at 10:28 AM 0
Share

@dmg0600 I don't see why searching for all the components is that bad as long as you don't do it in an update callback or in a coroutine, i.e. on every frame.

avatar image dmg0600 · Sep 23, 2014 at 10:37 AM 0
Share

It is bad if you do it often or for many objects, that is why I told if there are only a few objects it doesn't really matter.

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

26 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How to not rely on the Visual Interface 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How can an editor script know when another script was removed from the project? 1 Answer


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