«OnMouseDown» function and third-party script.

People, educate me!

(Right away I need to warn you – I’m «NOOB» both in «C Sharp» and in «Unity»)

How to use (or what analogue to use) function «OnMouseDown» from script that is not attached to the object on which you are clicking by mouse!?

Example: The script in which this function is prescribed is attached to the “MixButton” object, and I want to click on the third-party objects – «Button1», «Button2», «Button3», and so on. How to build a bridge between them!?

I know that “OnMouseDown” works only on the object to which is attached script with that function. Probably, there need to be any other way. My goal is to combine the scripts of a set of buttons on which it will be possible to click into one large, third-party, common script for all buttons. With “OnMouseDown” this can not be done. But the goal of my script is to repaint the “Cube2D” object in different colors (depending on which button I clicked on). Do not try to dissuade me from doing some other scheme for controlling the buttons. I do not care about the script itself and its functionality - I just puzzling out, testing, learning and want to know how to do it based on the goal, scheme. That is - how to click on objects (buttons) without attaching any scripts to them, but having one third-party script that will make “button” from the simple objects (graphics).

P.S. I hope nothing is clear, a lot of superfluous information, very long and nobody will help me…

using UnityEngine;
using System.Collections;

public class MixButton: MonoBehaviour {
   
    public GameObject Cube2D;
    public GameObject Button1;
    public GameObject Button2;
    public GameObject Button3;

    // -----------------------------------------------------------------------------------

    // BUTTON1:

    void OnMouseDown ()
    {
        SpriteRenderer renderer = Cube2D.GetComponent<SpriteRenderer>();
        renderer.color = Color.red;
        Debug.Log("red");
    }

    // -----------------------------------------------------------------------------------

    // BUTTON2:

    void OnMouseDown ()
    {
        SpriteRenderer renderer = Cube2D.GetComponent<SpriteRenderer>();
        renderer.color = Color.green;
        Debug.Log("green");
    }

    // -----------------------------------------------------------------------------------

    // BUTTON3:

    void OnMouseDown ()
    {
        SpriteRenderer renderer = Cube2D.GetComponent<SpriteRenderer>();
        renderer.color = Color.blue;
        Debug.Log("blue");
    }

    // -----------------------------------------------------------------------------------

}

I would recommend that you use the IPointerClick interface (or down) instead of those :slight_smile:
However, that is slightly off-topic.
What you can do is create a reference to the other script (and make sure you assign the reference, … maybe in the inspector is easiest).
Now, when you click your object, use your script variable to access the method. Make sure the method is public.

Also, if you are this new, you’d really benefit from going over some starter material in the learn section. Maybe an introduction short project and maybe a few Unity/scripting introduction tutorials :slight_smile: