How would I do SendMessage to a specific component on the same object?

What I’m trying to do is have a function send a message to the Check function, then have the Check function send a message back to that same function. This snippet gives no errors, but it still sends the message to all of the scripts, not the reciever. GetComponent isnt going to work because the name of the reciever is not known.

Thanks!

//This message is recieved from the event. We will respond wth CheckFailed if needed.
	public void Check (Component reciever) {
		if (failCheck) {
			reciever.SendMessage ("CheckFailed", "[TestCheck] Fail check was set to true.");
		}
	}

If the Component is on the same object, you can always cache the Component (script) and go through that.

For instance, if you have a script named ScriptA trying to get access to a variable in ScriptB…

// Inside script A declare a variable of the type ScriptB


private ScriptB scriptB_reference;


void Awake()
{
     // Get a reference to the script component in ScriptB
     scriptB_reference = GetComponent<ScriptB>();

     // Then whatever public variable you have in script B will be exposed.
     Debug.log(scriptB_reference.somePublicVariable);
}

Hope this helps.

SendMessage sends to every MonoBehaviour on the GameObject, so it won’t work if you only want to target one specific component.