Dialogue trigger when hitting a certain value
-
- Posts: 73
- Joined: Tue Jul 25, 2023 1:34 am
Dialogue trigger when hitting a certain value
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?
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
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":
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:
3. The first Dialogue System Trigger increments the TimesClicked variable:
4. The second Dialogue System Trigger has a condition that checks if TimesClicked is 7. If so, it plays a conversation:
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":
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:
3. The first Dialogue System Trigger increments the TimesClicked variable:
4. The second Dialogue System Trigger has a condition that checks if TimesClicked is 7. If so, it plays a conversation:
-
- Posts: 73
- Joined: Tue Jul 25, 2023 1:34 am
Re: Dialogue trigger when hitting a certain value
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
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
-
- Posts: 73
- Joined: Tue Jul 25, 2023 1:34 am
Re: Dialogue trigger when hitting a certain value
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?
how do I do the clicking set up without using a button?
Re: Dialogue trigger when hitting a certain value
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:
Let's assume the script has an OnMouseDown() method. At the bottom of the OnMouseDown() method, 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.
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!
}
-
- Posts: 73
- Joined: Tue Jul 25, 2023 1:34 am
Re: Dialogue trigger when hitting a certain value
This is how it activates the animation
Re: Dialogue trigger when hitting a certain value
Hi,
Below the "[SerializeField] private string animationName;" line, add this line:
In the OnTirggerActivate() method, add this line to the end:
(I put "..." in place of the rest of the code of that method since I didn't want to type it all out.)
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
Code: Select all
public override void OnTriggerActivate()
{
...
animationCoroutine = StartCoroutine(IResetToIdleState());
onClick.Invoke(); //<-- ADD THIS LINE
}
-
- Posts: 73
- Joined: Tue Jul 25, 2023 1:34 am
Re: Dialogue trigger when hitting a certain value
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
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
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.
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.