Page 1 of 3
Dialogue trigger when hitting a certain value
Posted: Wed Jul 26, 2023 3:22 am
by simplepleasuresdxy
Hi there,
I was hoping to inquire on what I could do about setting a dialogue trigger to activate after 7 clicks of something. I was thinking each click added to an unseen counter that would record how many times it was clicked and after 7 times, it would activate a dialogue, but only activate it once.
How could I go about that?
Re: Dialogue trigger when hitting a certain value
Posted: Wed Jul 26, 2023 8:42 am
by Tony Li
Hi,
Here's an example scene:
DS_Click7TimesExample_2023-07-26.unitypackage
To set it up:
1. In the Dialogue Editor window, add a Number variable. I called mine "TimesClicked":
- addVariable.png (18.71 KiB) Viewed 501 times
2. For my clickable object, I created a UI Button. I added two Dialogue System Triggers to this Button. They're both set to "OnUse". Then I configured the Button's OnClick() event to call both Dialogue System Triggers' OnUse() methods in order:
- clickableDialogueSystemTriggers.png (70.14 KiB) Viewed 501 times
3. The first Dialogue System Trigger increments the TimesClicked variable:
- dsTrigger1.png (49.78 KiB) Viewed 501 times
4. The second Dialogue System Trigger has a condition that checks if TimesClicked is 7. If so, it plays a conversation:
- dsTrigger2.png (76.92 KiB) Viewed 501 times
Re: Dialogue trigger when hitting a certain value
Posted: Wed Jul 26, 2023 11:47 am
by simplepleasuresdxy
This is awesome. Thank you!
Is there a way to link a unity variable to this in a similar way? I have a mechanic where players find runes and scanning them increases rune power by increments of one and i was envisioning a moment where a dialogue pops up after they get 3 magic power or something that tells them they can activate magic mode
Re: Dialogue trigger when hitting a certain value
Posted: Wed Jul 26, 2023 2:34 pm
by Tony Li
Re: Dialogue trigger when hitting a certain value
Posted: Wed Jul 26, 2023 8:30 pm
by simplepleasuresdxy
so ... looking through my code, I don't have a button per say. All I have is a game object with a box collider attached to it where by clicking in that box collider activates an animation ...
how do I do the clicking set up without using a button?
Re: Dialogue trigger when hitting a certain value
Posted: Wed Jul 26, 2023 9:30 pm
by Tony Li
Hi,
Does your GameObject have a script that handles the mouse click? If so, does it have a method named OnMouseDown() or OnMouseUp()? Either of these methods are a common way to handle mouse clicks.
If the script has either of these methods, then add this line of code in the top part of your script where you declare variables:
Code: Select all
public class AnExampleScript : MonoBehaviour
{
public UnityEngine.Events.UnityEvent onClick; //<--- ADD THIS LINE!
Let's assume the script has an OnMouseDown() method. At the bottom of the OnMouseDown() method, add this line:
Code: Select all
void OnMouseDown()
{
// (I'm guessing at how the animation is activated. This is just an example.)
GetComponent<Animator>().Play("Some Animation");
onClick.Invoke(); //<--- ADD THIS LINE!
}
This will add an OnClick() UnityEvent to the Inspector. You can configure it similarly to the UI Button's OnClick() UnityEvent above.
Re: Dialogue trigger when hitting a certain value
Posted: Thu Jul 27, 2023 2:27 am
by simplepleasuresdxy
This is how it activates the animation
Re: Dialogue trigger when hitting a certain value
Posted: Thu Jul 27, 2023 9:37 am
by Tony Li
Hi,
Below the "[SerializeField] private string animationName;" line, add this line:
Code: Select all
[SerializeField] private string animationName;
[SerializeField] private UnityEngine.Events.UnityEvent onClick; //<-- ADD THIS LINE
In the OnTirggerActivate() method, add this line to the end:
Code: Select all
public override void OnTriggerActivate()
{
...
animationCoroutine = StartCoroutine(IResetToIdleState());
onClick.Invoke(); //<-- ADD THIS LINE
}
(I put "..." in place of the rest of the code of that method since I didn't want to type it all out.)
Re: Dialogue trigger when hitting a certain value
Posted: Thu Jul 27, 2023 12:43 pm
by simplepleasuresdxy
Okay. Alright. I have this code for multiple objects. Do i have to make a seperate code so that other objects dont "invoke" it or do i specify that this object invokes something?
How does this additional code work with what younwere saying. I feel like im really close to understanding this
Re: Dialogue trigger when hitting a certain value
Posted: Thu Jul 27, 2023 4:13 pm
by Tony Li
Hi,
The code changes I suggested above add an OnClick() UnityEvent that you can configure in the inspector. If you don't assign anything to the OnClick() UnityEvent, it'll be just like you never added the code to the script in the first place.