I have been following “GameGrind’s” tutorial on yt, yet it seems i have a problem with one that may seem simple yet i have tried everything. I can not get the button to work.
I debugged it, Changed from button click to a mouseclick(as can be seen in code), it works, all functions work as intended. The only issue is when i try to use a delegate to make it work. when i click the button nothing happens, and i have no idea why.
I simply want the ContinueDialogue(); to run when i click the button.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogueSystem : MonoBehaviour {
public static DialogueSystem Instance { get; set; }
public List<string> dialogueLines = new List<string>();
public string npcName;
public GameObject dialoguePanel;
Button continueButton;
Text dialogueText, nameText;
int dialogueIndex;
// Use this for initialization
void BtnPressed()
{
Debug.Log("button has been pressed");
}
void Awake () {
continueButton = dialoguePanel.transform.FindChild("Continue").GetComponent<Button>();
dialogueText = dialoguePanel.transform.FindChild("Text").GetComponent<Text>();
nameText = dialoguePanel.transform.FindChild("Name").GetChild(0).GetComponent<Text>();
//continueButton = dialoguePanel.transform.FindChild("Continue").GetComponent<Button>();
continueButton.onClick.AddListener(delegate { ContinueDialogue(); } );
dialoguePanel.SetActive(false);
if (Instance != null && Instance !=this )
{
Destroy(gameObject);
}
else
{
Instance = this;
}
}//awake
public void CreateDialogue()
{
dialogueText.text = dialogueLines[dialogueIndex];
nameText.text = npcName;
dialoguePanel.SetActive(true);
}
public void ContinueDialogue()
{
if(dialogueIndex < (dialogueLines.Count -1))
{
dialogueIndex++;
dialogueText.text = dialogueLines[dialogueIndex];
}
else
{
dialoguePanel.SetActive(false);
}
}
public void AddNewDialogue(string[] lines, string npcName)
{
dialogueIndex = 0;
dialogueLines = new List<string>(lines.Length);
dialogueLines.AddRange(lines);
Debug.Log(dialogueLines.Count);
this.npcName = npcName;
CreateDialogue();
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(1)){
//dialoguePanel.SetActive(false);
ContinueDialogue();
}
}
}