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.
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!
– PippyLongbeard