- Home /
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.");
}
}
Answer by Stephan-B · Aug 27, 2012 at 09:13 PM
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.
Answer by Eric5h5 · Aug 27, 2012 at 08:49 PM
SendMessage sends to every MonoBehaviour on the GameObject, so it won't work if you only want to target one specific component.
Your answer
![](https://koobas.hobune.stream/wayback/20220613080254im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
more pragma strict getComponent and SendMessage problems 0 Answers
GetComponent for all enemies 1 Answer
pickup weapon with GetComponent 0 Answers
Best way to access variables of script from granchild of another object, into a different script? 1 Answer
Move a cube from GUITexture. Is not working. Where is my mistake 0 Answers