Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by awesomebuddy10 · Mar 12, 2018 at 01:12 PM · getmousebutton

How do you check if an instance of an object has been clicked on more than once?

I have 5 objects (which are different from each other), where I want to be able to do combos with them, such as tapping 3 of those specific objects will do something. But I only know how to check if 1 object has been clicked using c#.

New to unity. Help appreciated.

Comment
Add comment · Show 1
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 awesomebuddy10 · Mar 12, 2018 at 01:14 PM 0
Share

edit: Forgot to mention that by checking if 3 of those objects have been clicked consecutively in a certain amount of time, I want it to do something.

1 Reply

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

Answer by Hellium · Mar 12, 2018 at 02:03 PM

 1. Add an EventSystem in your scene (GameObject > UI > EventSystem)


 2. According to the type of your objects which must be clicked:

     a) 3D objects : Attach a Physics Raycaster to the event system

     b) 2D objects : Attach a Physics 2D Raycaster to the event system

     c) UI objects : Attach a Graphic Raycaster to the event system


 3. To each of the objects that should be clicked:

     a) 3D objects : Attach a Collider

     b) 2D objects : Attach a 2D Collider

     c) UI objects : Make sure the object has an image with RaycastTarget enabled


 4. Create a new C# script called ClickTarget.cs with the following class, and attach the component to each objects that should be clicked:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class ClickTarget : MonoBehaviour, IPointerClickHandler
 {
     public event System.Action<ClickTarget> OnClicked;
 
     public void OnPointerClick( PointerEventData eventData )
     {
         if ( OnClicked != null )
             OnClicked.Invoke( this );
     }
 }

 5. Create a new C# script called Combo.cs with the following class:

 using UnityEngine;
 
 [System.Serializable]
 public class Combo
 {
     public ClickTarget[] Targets;
     public UnityEngine.Events.UnityEvent OnComplete;
     public float maximumClickInterval = 0.5f;
 
     private int index = 0;
     private float lastClickTime;
 
     
     public void HandleTarget( ClickTarget target )
     {
         if ( Targets.Length < 1 || Targets[index] == null )
             return;
 
         // Reset timer if clicked too late
         if ( Time.time > lastClickTime + maximumClickInterval )
             index = 0;
 
         // Correct target clicked
         if ( target.GetInstanceID() == Targets[index].GetInstanceID() )
             index++;
         else if ( target.GetInstanceID() == Targets[0].GetInstanceID() )
             index = 1;
         else // Wrong target
             index = 0;
 
         if ( index == Targets.Length && OnComplete != null )
         {
             OnComplete.Invoke();
             index = 0;
         }
 
         lastClickTime = Time.time;
     }
 }


 6. Create an empty gameobject with the following ClickTargetsManager script:


 using UnityEngine;
 
 public class ClickTargetsManager : MonoBehaviour
 {
     public ClickTarget[] Targets;
     public Combo[] Combos;
 
     private void Start()
     {
         for ( int i = 0 ; i < Targets.Length ; i++ )
         {
             Targets[i].OnClicked += OnTargetClicked;
         }
     }
 
     private void OnTargetClicked( ClickTarget target )
     {
         for ( int i = 0 ; i < Combos.Length ; i++ )
         {
             Combos[i].HandleTarget( target );
         }
     }
 }

 7. In the inspector, select the ClickTargetsManager, and drag & drop all the objects holding the ClickTarget script into the Targets array


 8. Create your combos by dragging & dropping the gameobjects holding the ClickTarget script into the Targets array of each combo.


 9. Provide the function you want to call once a combo is completed the same way you do with the OnClick function of a button for example.

Screenshot


Please, next time, provide what you have tried so far


combos.png (390.8 kB)
Comment
Add comment · Show 8 · 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 awesomebuddy10 · Mar 12, 2018 at 07:30 PM 0
Share

Thank you. I appreciate it a lot. I was trying to use Get$$anonymous$$ouseButton(0), but it wasn't doing what I intended. I'll let you know how I get on

avatar image awesomebuddy10 · Apr 04, 2018 at 12:32 PM 0
Share

Sorry for asking for clarification with your detailed and easy to follow instructions, but do I provide my own function in the Combo script in the public method, or in the ClickTargets$$anonymous$$anager script?

Also I don't understand what the ClickTargets script and ClickTargets$$anonymous$$anager scripts are meant to do?

avatar image Hellium awesomebuddy10 · Apr 04, 2018 at 01:24 PM 0
Share

You have to provide the function you want to call in the Combo component. If you check the inspector, the OnComplete event on a Combo let you select a gameobject + function to call once the combo is completed.

The ClickTarget is just a script used to hook to the OnPointerClick function, sent by the Events$$anonymous$$anager of your scene. When the click is detected, it raises an event the ClickTargets$$anonymous$$anager is listening to.


This script listens to the OnClicked event of every target and "transmit" the click information to all the Combos. It's just a "manager" of all the individual targets, like a "conductor".

avatar image awesomebuddy10 Hellium · Apr 04, 2018 at 02:36 PM 0
Share

Oh, so I've modified the "public void HandleTarget() " method in the Combo script and I'm trying call it on OnComplete() in the inspector. Except, because it isn't attached to a game object and it doesn't have a mono behaviour, I have no way of calling it on OnComplete() ?

Thank you for helping me, I really appreciate the support.

Show more comments

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.

Follow this Question

Answers Answers and Comments

128 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 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

If Time > 0.25s 1 Answer

Issue with dragging 2D objects diagonally 0 Answers

Unity Error Script Vector2 0 Answers

Why is Input.GetMouseButtonUp(0) not returning true? 0 Answers

Pause my game with a game object! 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