Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Adam-Halley-Prinable · Feb 01, 2016 at 02:57 PM · functionexceptionsendmessagereceivertry-catch

How do I catch the SendMessage Has No Reciever exception thrown when I try to run a function with SendMessage that does not exist?

I'm making a game with a command line where you can tell different objects to run different functions.

If they send a function call to an object that doesn't exist, I give them a custom error message through my command line.

But when they send a function that doesn't exist to an object that does, I can't catch the exception. In the following code, noun is the object and verb is the command. For example, open door is split into verb, noun respectively.

 if (GameObject.Find(noun.ToUpper()))
             {
                 try
                 {
                     GameObject.Find(noun.ToUpper()).SendMessage("COM_" + verb, password);
                     jukebox.PlayOneShot(jukebox.library["command_accepted2"]);
                 }
                 catch//THIS NEVER RUNS
                 {
                     Print("ERROR: " + noun + " does not have '"+verb+"' as a function.");
                     jukebox.PlayOneShot(jukebox.library["command_rejected2"]);
                 }
             }
             else
             {
                 Print("ERROR: Object '" + noun.ToUpper() + "' was not found.");
                 jukebox.PlayOneShot(jukebox.library["command_rejected2"]);                
             }

How do I catch the "SendMessage has no receiver!" exception, OR how do I detect beforehand if an object has the verb function within?

inb4 don't use sendmessage, in my case it's the best option.

Comment
Add comment · Show 4
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 Munchy2007 · Feb 01, 2016 at 03:12 PM 0
Share

Send$$anonymous$$essage doesn't raise an exception as such when no receivers are found, it just prints an error message.

I guess you could use reflection to check if the verb function exists.

Despite your inb4 comment, I can't help thinking that interfaces might be a better approach.

avatar image Adam-Halley-Prinable Munchy2007 · Feb 01, 2016 at 04:28 PM 0
Share

Could you suggest how reflection could do that in the form of an answer please?

avatar image Owen-Reynolds Adam-Halley-Prinable · Feb 01, 2016 at 04:54 PM 1
Share

Reflection is a standard C# feature. You should be able to find lots of non-Unity "find all the functions" examples, which are much better than anyone is going to quickly write for you here.

avatar image hexagonius · Feb 01, 2016 at 09:50 PM 0
Share

I'd suggest keeping possible commands in a string array or a dictionary with reference to what to do. It's easier editable, you can just iterate the list to see if the command exist, populate it in the inspector for easy adjustment...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AlienNova · Oct 26, 2017 at 09:07 PM

Sorry to necro a thread but since I ended up here so might some other people.

You can't try SendMessage () and catch if it throws a no receiver error. The best thing I found was to try dynamically invoking a message and catching the error through it. https://www.codeproject.com/Articles/19911/Dynamically-Invoke-A-Method-Given-Strings-with-Me

How ever this is extremely circumventive so I really don't like doing it, but it's the best I could find.

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

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

Related Questions

IAP generates exception when calling m_StoreController.InitiatePurchase(product) 0 Answers

Calling a function in another script not working with yield 1 Answer

Try Catch not working with VideoPlayer 2 Answers

Error on my javascript code 1 Answer

Calling a function from a different script on the same object 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