- Home /
Make a function give different outputs depending on amount of left-clicks.
So, my question is if it's possible for a function of a class I made to give a different output depending on the amount of left mouse clicks it gets. Specifically, I'm trying to make a dialogue that changes when the character clicks his mouse, and gets the dialogue sentences from the inspector. The script I've got so far allows the player character to click on an NPC and get a sentence. However, I can't figure out how to change the sentence he gets, simply because I keep having to run the function of my class every time the player clicks the mouse again, resetting all the variables and such. I get an error every time I try to use variables declared at the beginning of the script to store the sentences. If someone could point out how I might accomplish this, I'd be very appreciative. Code: using UnityEngine; using System.Collections;
public class Dialogue : MonoBehaviour {
public string sentence1;
public string sentence2;
RaycastHit rayHit;
string greetString;
bool runGUI = false;
bool GUIactive = false;
public GameObject player;
public GUISkin guiSkin;
bool fin1 = false;
bool fin2 = false;
public class DialogueClass
{
public string getDialogue(RaycastHit rayTarget, string sen1, string sen2)
{
string conversant = rayTarget.collider.name;
if (conversant == "Man1")
{
return sen1;
}
if (conversant == "Man2")
{
return sen2;
}
else
{
return "";
}
}
}
DialogueClass TestDialogue = new DialogueClass();
void Start () {
}
void Update () {
Vector3 fwd = transform.TransformDirection (Vector3.forward);
if ((Physics.Raycast (transform.position, fwd, out rayHit, 5)))
{
if (Input.GetKeyDown ("mouse 0"))
{
greetString = TestDialogue.getDialogue (rayHit, sentence1, sentence2);
GUIactive = true;
if (GUIactive)
{
runGUI = true;
}
}
}
else
{
runGUI = false;
GUIactive = false;
}
}
void OnGUI () {
GUI.skin = guiSkin;
if (runGUI)
{
GUI.Box (new Rect(0,Screen.height * 0.8f,Screen.width,Screen.height * 0.2f), greetString);
}
}
}
By the way, the 'GUIactive' boolean is there because if I don't use it, the 'Input.GetKeyDown' function causes the dialogue to flicker there for a fraction of a second before disappearing. Very unclean, but I can't figure out another way to do it. Thanks in advance.
Answer by CoalCzar · Nov 28, 2013 at 05:28 AM
If I understand you correctly, I would declare a variable in your Dialogue class to hold the number of clicks and then pass it as a parameter to your DialogueClass function.
// declare this with your other Dialogue : MonoBehaviour class variables
private int numOfClicks;
// add a parameter for numOfClicks in your getDialogue function
public class DialogueClass
{
public string getDialogue(RaycastHit rayTarget, string sen1, string sen2, int clicks)
{
string conversant = rayTarget.collider.name;
if (conversant == "Man1")
{
//Check for the clicks here
if(clicks == 1)
return sen1;
else
// return something else or check for a different number of clicks
}
if (conversant == "Man2")
{
return sen2;
}
else
{
return "";
}
}
}
// Add to numOfClicks in your Input check for mouse clicks.
void Update () {
Vector3 fwd = transform.TransformDirection (Vector3.forward);
if ((Physics.Raycast (transform.position, fwd, out rayHit, 5)))
{
if (Input.GetKeyDown ("mouse 0"))
{
// increment numOfClicks
numOfClicks++;
// pass numOfClicks as an argument
greetString = TestDialogue.getDialogue (rayHit, sentence1, sentence2, numOfClicks);
GUIactive = true;
if (GUIactive)
{
runGUI = true;
}
}
}
else
{
runGUI = false;
GUIactive = false;
}
}
Ah, thank you. For some reason, I was thinking that the numOfClicks variable would reset every time I called the getDialogue() function from Update(). Anyways, thanks!
Your answer
