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.
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.
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.
Please, next time, provide what you have tried so far
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
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?
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".
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.
Your answer
Follow this Question
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