- Home /
Recognizing a Mouse Click between Scripts
I'm new at this, I've been trying to write a script on a 3D Text object that will recognize when another game object (a cube) has been clicked and change its text accordingly. My end goal is to have the text be a counter that increments each time the cube is clicked.
My question is: What is the simplest way to detect a click on the cube from within a script that is attached to the text object?
Initially I had a script attached to my cube that would change a boolean from false to true in the body of OnMouseDown(). Then I tried to create an instance of that cube class in another script to see if the mouse had been clicked and change the text.
I was not able to get this to work. Is there a simpler way to do this? Could/Should this be done in two scripts or one? Thank you for your help.
Answer by karljj1 · Mar 17, 2014 at 11:44 PM
Have a variable to the text GameObject in your script that is on the cube. You can then assign the text to this variable in the inspector.
E.G This goes on your cube.
public GameObject myText;
void OnMouseDown()
{
// Send the message to the text
myText.SendMessage( "OnMouseDown" );
}