This is very puzzling! I’m working on a quest system and have chosen to use c# events. I have a quest giver script placed on a model, I have a quest panel that tells the player about quest and gives them the choice via button to accept the quest or not( yes button and no button). I also have a journal script to keep track of current and completed quests. I set up a delegate and event for OnAcceptQuest. Just as a test, I set up a keypress to add the quest, which worked. Only when I try using the button, it didn’t work at all; the quest was getting lost so I found a way around that, I thought. Now I’m getting a Stack Overflow error when I try to add the quest via click on a button image. This post is a little long-winded, but I am trying to give as much info as possible. Now for the code:
Event Manager:
public class EventsManager
{
//Quests
public delegate void QuestAddDelegate(QuestClass quest);
public static QuestAddDelegate OnAcceptQuestEvent;
public static void AcceptQuest(QuestClass quest)
{
if(OnAcceptQuestEvent != null)
{
OnAcceptQuestEvent(quest);
}
}
}
Quest Giver:
private void OnTriggerEnter()
{
print(“Open panel”);
OpenQuestPanel();
}
private void OnTriggerExit()
{
print("Close panel");
CloseQuestPanel();
}
private void OnEnable()
{
EventsManager.OnAcceptQuestEvent += AddQuest;
}
private void OnDisable()
{
EventsManager.OnAcceptQuestEvent -= AddQuest;
}
public void OpenQuestPanel()
{
if(Quest.questStatus == QuestStatus.Unassigned)
{
QuestPanel.SetActive(true);
PresentQuest();
}
else if(Quest.questStatus == QuestStatus.InProcess)
{
QuestPanel.SetActive(false);
ProgressRewardPanel.SetActive(true);
QuestInProgress();
}
else if (Quest.questStatus == QuestStatus.Complete)
{
Rewards();
}
}
public void CloseQuestPanel()
{
QuestPanel.SetActive(false);
}
void PresentQuest()
{
QuestPanel.SetActive(true);
IntroText.text = Quest.introText;
QuestDescText.text = Quest.questDescription;
RewardText.text = "You will receive " + Quest.reward + " for your trouble.";
}
//public void OnClickYes()
//{
//questAccepted = true;
// Debug.Log("Yes, accept the Quest!");
// AddQuest(Quest);
//checkquest();
// Add quest to the active quest list
// change icon to hourglass
//}
public void OnClick()
{
Debug.Log("Yes, accept the Quest!");
AddQuest(Quest);
}
public void OnClickNo()
{
RefusalText.text = Quest.refusalText;
}
void AddQuest(QuestClass quest)
{
EventsManager.AcceptQuest(quest);
Quest.questStatus = QuestStatus.InProcess;
}
}
Journal:
private void OnEnable()
{
EventsManager.OnAcceptQuestEvent += AddQuest;
EventsManager.OnCancelQuestEvent += RemoveQuest;
}
private void OnDisable()
{
EventsManager.OnAcceptQuestEvent -= AddQuest;
EventsManager.OnCancelQuestEvent -= RemoveQuest;
}
void Start()
{
}
void Update()
{
}
void AddQuest(QuestClass quest)
{
CurrentQuestList.Add(quest);
print(CurrentQuestList.Count);
ShowCurrentQuests();
}
void RemoveQuest(QuestClass quest)
{
CurrentQuestList.Remove(quest);
print(CurrentQuestList.Count);
}
Test script:
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
print("Q");
EventsManager.AcceptQuest(quest);
}
Thanks in advance!