OnMousedown for multiple sprites

Hello, i have a matrix of multiple sprites.
Can i add the onmousedown function to each sprite (with collider) automatically with a C# script?

Just like Onmousedonw(sprite1)…Onmousedownsprite(XXX)?

It’s very awkward to do it by hand.

If you want only the object which is your mouse over to runs the function, you need a Raycast to solve your problem.

But, if you want every object affects same when you click your mouse button. You need to add your script to every object. So you can use these scripts I write for you.

AutoAttach.cs :

using System;
using UnityEngine;

public class AutoAttach : MonoBehaviour {
    public Type scriptType;
    public string scriptName;
    public Transform root;

    void Start()
    {
        scriptType = Type.GetType(scriptName);
    }

    public void Attach()
    {
        if (root == null)
        {
            Debug.LogWarning("The root can't be empty!");
            return;
        }

        foreach (Transform item in root)
        {
            // don't has, add
            if (!item.gameObject.GetComponent(scriptName))
            {
                item.gameObject.AddComponent(Type.GetType(scriptName));
            }
        }
    }

    public void Remove()
    {
        foreach (Transform item in root)
        {
            // has, remove
            Component cop = item.gameObject.GetComponent(scriptName);
            if (cop)
            {
                DestroyImmediate(cop);
            }
        }
    }
}

AutoAttachEditor.cs : (Put it in an Editor folder, if don’t have it, create one)

using UnityEngine;
using UnityEditor;
using System;

[CanEditMultipleObjects, CustomEditor(typeof(AutoAttach))]
public class AutoAttachEditor : Editor {

    public SerializedProperty scriptName;
    public SerializedProperty root;
    AutoAttach autoAttach;
    private void OnEnable()
    {
        autoAttach = target as AutoAttach;
        scriptName = serializedObject.FindProperty("scriptName");
        root = serializedObject.FindProperty("root");
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUILayout.PropertyField(scriptName, new GUIContent("ScriptName"));
        autoAttach.scriptType = Type.GetType(scriptName.stringValue);
        EditorGUILayout.PropertyField(root, new GUIContent("Root"));
        
        serializedObject.ApplyModifiedProperties();

        if (GUILayout.Button("Attach"))
        {
            autoAttach.Attach();
        }
        GUILayout.Space(10);
        if (GUILayout.Button("Remove"))
        {
            autoAttach.Remove();
        }
    }
    
}

So, the first thing you have to do is put your multiple sprites as a child of a root, and drag the AutoAttach script to your root object.

Then you have to set the Root value and the ScriptName value(it means which Script you want to every child attached to, so you have to input your script name here.)

When you click the Attach button in the Inspector window, you can attach your script to every child.

Remove button is in reverse.

Hope my scripts can help you.

119380-hierarchy.png

Good day.

You can create a script called for example “sprite clicks” with that function. And add it at all the obejcts as a component. checking the sprites name, or position, or tag or somethign to choose what to do.

Or you can do it via Raycast, and know what sprite are you clicking.

Go watch some Raycas tuttorials and will learn how to do it :D!

Bye!