• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by knuppel · Jun 22, 2018 at 03:37 PM · onmousedown

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by LeeroyLin · Jun 22, 2018 at 07:01 PM

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.

alt text

alt text


hierarchy.png (4.0 kB)
inspector.png (23.3 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image riskaanisah · Nov 14, 2018 at 10:14 PM 0
Share

hello if I may ask. I have a problem with if statment on onmousedown. they can't read which button 1.2 or 3 is clicked. how should I handle it, please help me`public GameObject karakter;

 public GameObject ButtonLevel1;
     public GameObject ButtonLevel2;
 
     private void On$$anonymous$$ouseDown()
     {
         if (ButtonLevel1) {
             Debug.Log("a");
         }
 
         if (ButtonLevel2)
         {
             Debug.Log("b");
         }`
     }

avatar image
0

Answer by tormentoarmagedoom · Jun 22, 2018 at 03:43 PM

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

86 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnMouseDown to return object name 3 Answers

On Mouse Down to trigger OnGUI function 2 Answers

Reverse Animtion On clicking text. 2 Answers

How to turn on/off multiple lights using GUI buttons? 1 Answer

removing chracter 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges