- Home /
Quests/Dialogue (C#)
So i want to make a quest. i have a select script and i have a dialogue box. now i need to connect the two so that when a specific object is selected and then you right click the dialogue will pop up. i ran into 1 problems when i was trying to make this script 1. you are pushing more gui clips then you are popping. this is how the scrip looks for the dialogue
using UnityEngine;
using System.Collections;
public class Speek : MonoBehaviour {
// Use this for initialization
public void Start ()
{
CurrentMenu = MainMenu;
DisplayMenu = false;
Dad = false;
}
public void Update()
{
if (Input.GetMouseButtonDown(1))
DisplayMenu = !DisplayMenu;
if (Input.GetKeyDown("q")){
Dad = true;
}
}
public void OnGUI()
{
if (DisplayMenu)
CurrentMenu();
if(Dad)
dad();
}
private delegate void MenuDelegate();
private MenuDelegate CurrentMenu;
private bool DisplayMenu;
public bool Dad;
public string nameOfGiver;
public string nameOfDone;
public string QuestPage1;
public string QuestPage2;
public string Answer;
private bool Quest = false;
private bool DadDone = false;
//
// Main menu
//
private void MainMenu ()
{
Rect MainMenuRect = new Rect((Screen.width - 280) / 2, (Screen.height - 100) / 1.03f, 630, 108);
GUI.BeginGroup(MainMenuRect);
GUI.Box(new Rect(10, 10, MainMenuRect.width, MainMenuRect.height), nameOfGiver );
GUI.Label(new Rect(25, 25, 540, 80), QuestPage1 );
if (GUI.Button(new Rect(540, 80, 80, 20), "Next"))
{
CurrentMenu = Next;
}
}
private void Next(){
Rect OptionsRect = new Rect((Screen.width - 280) / 2, (Screen.height - 100) / 1.03f, 630, 108);
GUI.BeginGroup(OptionsRect);
GUI.Box(new Rect(10,10,OptionsRect.width,OptionsRect.height), nameOfGiver );
GUI.Label(new Rect(25, 25, 540, 80), QuestPage2 );
if (GUI.Button(new Rect(15, 80, 80, 20), "Back"))
{
CurrentMenu = MainMenu;
}
if (GUI.Button(new Rect(540, 80, 80, 20), "Okay"))
{
DisplayMenu = false;
}
}
private void dad(){
Rect OptionsRect = new Rect((Screen.width - 280) / 2, (Screen.height - 100) / 1.03f, 630, 108);
GUI.BeginGroup(OptionsRect);
GUI.Box(new Rect(10,10,OptionsRect.width,OptionsRect.height), nameOfGiver );
GUI.Label(new Rect(25, 25, 540, 80), Answer );
if (GUI.Button(new Rect(15, 80, 80, 20), "Back"))
{
CurrentMenu = MainMenu;
}
if (GUI.Button(new Rect(540, 80, 80, 20), "Okay"))
{
DisplayMenu = false;
Quest = true;
}
}
}
}
}
}
and this is how the targeting script looks like
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TargettingQuest : MonoBehaviour {
public List<Transform> targets;
public Transform selectedTarget;
public GameObject target;
private Transform myTransform;
// Use this for initialization
void Start () {
targets = new List<Transform>();
selectedTarget = null;
myTransform = transform;
AddAllEnemies();
}
public void AddAllEnemies() {
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in go)
AddTarget(enemy.transform);
}
public void AddTarget(Transform enemy) {
targets.Add(enemy);
}
private void SortTargetsByDistance() {
targets.Sort(delegate(Transform t1, Transform t2) {
return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
});
}
#region
//This is added by adrian
#endregion
//if we do not have an enemy targeted ywt, then find the clostest one and target him
//if we do have an enemy targeted, then get the next target
//if we have the last target in the list, then get then first target in the list
private void TargetEnemy() {
if(selectedTarget == null) {
SortTargetsByDistance();
selectedTarget = targets[0];
}
else {
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count - 1) {
index++;
}
else {
index = 0;
}
DeselectTarget();
selectedTarget = targets[index];
}
SelectTarget();
}
private void SelectTarget() {
selectedTarget.renderer.material.color = Color.red;
}
private void DeselectTarget() {
selectedTarget.renderer.material.color = Color.white;
selectedTarget = null;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Tab)) {
TargetEnemy();
}
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if(distance < 2.5f) {
if(direction > 0) {
Speek pa = (Speek)GetComponent("Speek");
pa.Dad = true;
}
}
}
}
How can i set the distance so i cant select the object when it more then 1 yrd away from me?
how can i make it so when i press okay the quest begins .. ( in this case the quest asks you to go talk to your dad. so i want it to be that if you talk to your dad before you get the quest noting should happen and then when you press okay another script activates that lets you to clame your reward.
So...I cleaned up your code a bit but it's still kind of a mess to read. In the future, please try to make it easier on everyone by using proper indentation and $$anonymous$$imizing the amount of line breaks in whatever you post.
each call to gui.begingroup() needs to be closed by a gui.endgroup(), which comes after the last gui element you want in the group. that will fix the "pushing more gui clips" error that you're having.
To 'activate' your quest you can declare a bool and flag it as true after the player clicked 'okay'. When they start conversation with the father you check to see the state of that bool and pop the appropriate dialog/script.
So I put the completion script and the dialogue script in to two different scripts. how can i link them so that when i press okay it makes the variable in the other script turn true?
Your answer
Follow this Question
Related Questions
Creating a Quest system 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
I need help understanding why this variable returns null. 1 Answer
Character Selection Scene Help 1 Answer