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 Mel Zombie · Oct 15, 2014 at 06:12 PM · methodbooimplementation

How to check if a certain method is implemented

Hi! I'm making a classical point&click quest and need to know if some object can be pushed/used/etc. Main camera object script raycasts such objects and stores a reference to them. Player object script has main camera as a public variable and asks it for that reference. And at this point I don't know how should I proceed further. First, it seems like I cannot use a reference to a reference like this cameraScript.clickedObject.someMethod(). It logs an error for some reason. Second, if I use cameraScript.clickedObject.SendMessage("someMethod") I need to know right away if that method is implemented. I thought maybe SendMessage returns true/false for success/failure or some error object can be used but found none in the documentation. Thanks!

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

3 Replies

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

Answer by Kiwasi · Oct 15, 2014 at 06:15 PM

You can use reflection to discover if a method exists.

Comment
Add comment · Show 2 · 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 Mel Zombie · Oct 15, 2014 at 08:16 PM 0
Share

What is reflection? Is it unity or boo feature? A link would be great

avatar image Kiwasi · Oct 15, 2014 at 10:49 PM 0
Share

Reflection is a .NET feature. Here is the link to the documentation. It works in C#, not sure where you would go for documentation in JavaScript. You really don't want to touch Boo, the language only exists for historical reasons.

Be warned this is some pretty heavy coding. If the GetComponent solution works as suggested by @Julianobsg then use it. OOP constructs like interfaces and inheritance often can be used as well. But if you must be able to detect if a method is implemented on a object without knowing details of the object in advance then reflection is the way to go.

avatar image
1

Answer by Mel Zombie · Oct 17, 2014 at 01:22 PM

Here is how I've done it:

if cameraScript.clickedObjectScript != null and cameraScript.clickedObjectScript.GetType().GetMethod("method"): cameraScript.clickedObjectScript.SendMessage("method")

A script per command also works fine if overly complicated in my case. Thanks to everyone!

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

Answer by Julianobsg · Oct 15, 2014 at 06:51 PM

Use a

 if(gameObject.GetComponent<Pushable>() != null)

and will return true or false if there is any component of that class there.

You don't need to use a single class for everything, you can create a class and add that property to any go, and is cleaner sometimes. You can also check a tag, if you put a tag pushable in the go for example.

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 Habitablaba · Oct 15, 2014 at 11:34 PM 1
Share

Just to be clear, if you use GetComponent, you should really not have to use Send$$anonymous$$essage as well. You should be able to something like the following:

 // wherever your camera / clicked object stuff is
 var clicked = camera.ClickedObject.GetComponent<SomeType>();
 if(clicked != null)
 {
   clicked.Some$$anonymous$$ethod();
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Invalid IL code exception when calling method 2 Answers

Boo bug? BCE0046: is can t be used with a value type ( UnityEngine.NetworkPlayer ) 1 Answer

Check for empty space relative to an object 1 Answer

How do I indicate that an Array is an Array? 1 Answer

Webplayer exception when using BufferedStream 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