How can I detect/catch the SendMessageOptions.RequireReceiver error message?

SendMessage is kind of antiquated. The way to do things today is to make an interface.

That way you say “is there this interface?” and check if it’s not null, then call the method you want if it is.

Don’t be afraid, it’s pretty simple, and it’s way more powerful. Here, soooper-quickly:

// stick this in its own file, usually IMyInterface.cs
public interface IMyInterface
{
  void MyMethod( int arg1, string arg2);
}

Now, in whatever Monobehaviors you have, you implement that interface, which basically involves:

  1. put that interface after a comma right after MonoBehavior in the class header:
public class FooClass : Monobehavior, IMyInterface
{
}
  1. make a public version of the above function in the FooClass, with body:
public void MyMethod( int arg1, string arg2)
{
// do your stuff!
}

Now, to find this interface you say:

var imyInterface = GetComponent<IMyInterface>();

and to potentially call it you do:

if (imyInterface != null)
{
  imyInterface.MyMethod( 1, "yabo!");
}

That’s it! Read more if you like but that is the tl;dr of using C# interfaces in Unity3D.

4 Likes
How do I only check for specific game objects in a BoxCast?
Weapons system
Kindly Requesting Guidance for My Scripting Structure
Where in the execution order does unityInstance.SendMessage get fired from WebGL?
Convert Parent class to Child class from a method
How to make the architecture of the character doll?
Have an object contain a movement monobehaviour but let a different object execute it?
Best way to have queued move actions for up to 10 players executing simultaneously?
Calling functions from a separate, unique script using a ray cast.
Dealing with so many interactable objects
How to get static game data for a subclass
Referencing a Script through a Public String with GetComponent
Using a script as a class without object
Advice Needed for Monopoly Style Board Game
Calling function XXXX with no parameters but the function requires 1.
Will my idea for organizing different enemies work and is it best practice?
What are C# Interfaces? Can someone explain them in plain English?
(Suggestion) Which programming design is better for damaging the enemy?
Setup Enemy Class proper coding standard question.
How to make the architecture of the character doll?
How to implement Different weapon stats? (Best Practice?)
Issues with abstract members
Need help in turning mass chunk of code into smaller and more efficient ones
List of Scripts to Execute Issues
need some help with trigger/colliders and where/how to put them for an item pickup
Getting object hit by RayCast
Invoke Method Script
Pass Type In MonoBehaviour From Other Script
Add abilities to different unit types
How do I avoid a bunch of if statements and avoid a switch
Inheritance problem
Should I extract this duplication into a superclass, interface, or separate component?
Colliders from multiple interactable objects
Attaching a Script to a ScriptableObject
Suggestions for using interfaces in Card Battler like Slay the Spire?
call Script with the same name but other code
Passing SO from one Script to another. (Question)
Why is my send message not working?
Understanding Interfaces. IPointerDownHandler
Identifying unittype among many different
How do I make a script call a variable from many instances of another script
InvalidCastException: Specified cast is not valid.
Problem getting reference to a specific class
How to get a variable value from another script(C#) without setting script name.
Question on approach for a top down 2d click to move/attack/interact game
How to check if a gameobjects class is implementing an interface
Same OnEnable()/OnDisable() on multiple scripts? Calling same method name from one script
Architecture of a Spell System
Best way to decouple skill system from the other systems it influences?
Want to use interface for lootable items but don't know where to start
Finding array of current state game objects
How to make a list that can contain different variable types?
Setting a general code to access inherited variables for Buffs/Debuffs system
Is my approach is good for generics?
Is there other way around?
Create Inspector UI for parent class that changes depending on the type of child
Question on a (more or less) complex inventory/item system
How to make one gameobject immune to a script attached to a separate gameobject
Dealing with multiple types of items for an inventory
Incorporate a new functionality in many gameobject
Ambiguous/Universal Object Activation
Is it possible to cast as a Type from a string?
Get variable from other script with unknown name
How to do map colliders for 2D pixelart game?
Help with syncing multiple types of derived class using netcode for gameobjects