0

I'm trying to make it so that once you click "Accept" on a UI button that it will store all the values from the quest into the following script:

[System.Serializable]
public class DataHolder
{
    //Active Quest
    public bool isActive;
    public string title;
    public string description;
    public int goldReward;
    public QuestGoal goal;
    
}

The script that stores it is here:

public class QuestGiver : MonoBehaviour
{
    public Quest quest;
    
    public DataHolder data;
        
    public GameObject questWindow;
    public Text titleText;
    public Text descriptionText;
    public Text goldText;
    
    public void OpenQuestWindow()
    {
        Debug.Log("Quest Window Opened");
        questWindow.SetActive(true);
        titleText.text = quest.title;
        descriptionText.text = quest.description;
        goldText.text = quest.goldReward.ToString();
    }
    
    public void AcceptQuest()
    {
        questWindow.SetActive(false);
        data.isActive = true;
        data.title = quest.title;
        data.description = quest.description;
        data.goldReward = quest.goldReward;
    }
}

and the script that is in the scene so that when a button is pressed (Tab) it will pop up a UI that will show what the current quest is, which should be stored in the first "DataHolder" script:

public class QuestChecker : MonoBehaviour
{
    DataHolder data;
    
    public GameObject questWindow;
    public Text titleText;
    public Text descriptionText;
    public Text goldText;
    
    
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            CheckQuest();
        }
    }
    
    public void CheckQuest()
    {
        questWindow.SetActive(true);
        
            titleText.text = data.title;
            descriptionText.text = data.description;
            goldText.text = data.goldReward.ToString();
        
        
    }
    
    public void CloseQuestWindow()
    {
        questWindow.SetActive(false);
    }

}

The reason I want it stored in "DataHolder" is because I want it to be stored between scenes. I believe my main problem is not knowing how to properly reference the "DataHolder" script as Unity gives me the warning:

Assets\Scripts\QuestChecker.cs(8,13): warning CS0649: Field 'QuestChecker.data' is never assigned to, and will always have its default value null

Any help would be appreciated. I'm pretty new to programming, please let me know if this won't work for what I want it to do and need to do it another way or if you need more info. Thanks

almand
  • 27
  • 4
  • `DataHolder data = new DataHolder();` ? Besides that does this answer your question: [How to pass data between scenes in Unity](https://stackoverflow.com/questions/32306704/how-to-pass-data-between-scenes-in-unity) – derHugo Oct 13 '20 at 07:28

3 Answers3

1

If all you want to do is store some data between scenes, you could simply do this:

public static class DataHolder
{
    //Active Quest
    public static bool isActive;
    public static string title;
    public static string description;
    public static int goldReward;
    public static QuestGoal goal;    
}

Then just reference it as DataHolder.isActive etc. Note that you're not actually "saving" the data anywhere (serialising). The SerializableAttribute just indicates that the item CAN be serialised. But, this WILL store data between scenes.

Is it the best way to do so? Well, if it's just some simple data you need to store between scenes, then there's nothing wrong with doing it this way. And it'll probably fit in nicely with what you've already programmed.

Milan Egon Votrubec
  • 3,696
  • 2
  • 10
  • 24
0

You're never actually creating an instance of DataHolder.

Somewhere along the line you need a new DataHolder() that is assigned to data before trying to set any of its properties.

FriedLychees
  • 114
  • 1
  • 9
0

You need to create a relationship between QuestGiver and QuestChecker

eg.

public class QuestChecker : MonoBehaviour
{
    public QuestGiver giver;
}

Now drag your QuestGiver game object onto the QuestChecker.giver field in the inspector. Then you are able to access your saved data.

public void CheckQuest()
{
    titleText.text = saver.data.title;
}
shingo
  • 18,436
  • 5
  • 23
  • 42