Populating a Question Pool with Lists upon Lists

For my quiz, I’m wanting to populate my quiz questions from a series of tagged scriptable game objects. Its a flash card game for language learning, so for every “question” (word), I have included different tags in the form of a list of strings. Categories include “Food” “numbers” “colors”, etc.[Each one has more than one tag/category/keyword, which is why I chose to do it this way]. So if I decide I want to quiz myself on just words related to food, I want to be able to hit a button in my menu that says “food” which will include that category. If I want to quiz Food and Numbers, I can hit “food” & “numbers” buttons.

The Logic I’m considering is, when I click the button, I grab the text from that button and add that text to a list of strings(?). Then using that list of strings, I can do a foreach loop that iterates through the indices of that list, adding all the SOs’ words that contain any of those chosen tags to a new list (the question pool) to be quizzed on when I start the lesson.

I’ve tested a short easy version to see if what I have so far will populate the new list and it works. What I’m having a hard time with is how to store that text when I click the button.

I have a Question Manager script which is where I was going to store the method, but I can’t think of how it stores the text from the click. The only way I know how to access things is by making a variable and then dragging and dropping game assets into that slot in the inspector. I’ve tried googling it and I get the same thing I just mentioned with the variables and filling it in in the inspector. I got something about EventSystems.current.current-something but I couldn’t get it to work and I think that was just the name of the asset, not the text in the field.

Anyways. I’m probably overthinking it and now I’m frustrated. So any help would be appreciated.

Hi SpiffyMai,

It’s great to see that you are challenging yourself, and your description sounds like a solid concept.

Since you have different categories, it could make sense to create an array for each category. Depending which category is “active” in your game, your code picks questions from one of those arrays. The rest should work like Rick’s version.

What do you mean by “store”? If you have code snippets showing how you imagine the logic of your game, feel free to share them here. Pseudo-code would also be fine if you do not have valid C# code.

Button > Inspector > OnClick() > Question Manager > GetButtonText()

List<String?> categoriesToAddtoQPool = new List<string?>;

GetButtonText()
{
Store the Text from the Button I just clicked on (TMP?) in variable buttonTextToAdd;
buttonTextToAdd.Add(categoriesToAddtoQPool);
}

So I want to grab this text:

and make that method up there plugged into this:

And this, for funsies, is what I did to test if the keyword was gonna work:

![GDTV_03|577x500](upload://aU855wggFafewP7go4Y6iFtb7cu.png

I don’t know how to store a reference to that button text from the button click. How to get from “I clicked this button” to “store the reference to the button text of the button I just clicked so I can add the text to the list of strings.” And I don’t know what type it is. I thought it would be a string but I don’t know if I’m going to need to convert it to a string. Everything I’ve tried gives me squiggles, and I just don’t know how to tell it to grab the information I want (button’s text) from the game object I just clicked on.

I don’t even know if the way I wrote it in pseudo code is even the way to go about it. It’s just the format I’ve learned so far.

I currently do not know if it is possible to define the method with a Button parameter. If that’s possible, you could probably assign the button component by dragging and dropping to the field. In Gary’s game, he defined his method with an integer parameter, and he assigned an integer value in the OnClick box.

If you cannot create a field which takes in a reference because Unity does not support that, I would suggest to use integers. Integers do not have any inherent meaning, so you could give them a purpose within your concept. If the button is always associated with a category, and if your questions are also associated with a category, I would suggest to create a data structure which makes both things a unit.

Maybe this works. Here is the data structure I mean:

using UnityEngine;

[System.Serializable]
public class Category // perhaps you'll have to add ': MonoBehaviour'
{
    [SerializeField] Button button;
    [SerializeField] QuestionSO questionSO;
 }

Of course, it might be that this is not what you need. You could use it as a template, though. In your QuestionManager or wherever you handle the logic of your game, you create an array of type Category and expose it in the Inspector. Then you populate the elements of the array.

And then you use the integer. At index 0, the first element would be a Category, which contains your button and whatever you want. And if you have a reference during runtime, you can usually also change the values during runtime (unless something is readonly or const).

Since I am not sure if I understood your concept and ideas correctly, do not follow my advice without evaluating whether this makes sense within the context of your project. I just answered your question, or at least, what I thought was the question.

I hope this helps. :slight_smile:

So it was pretty boilerplate. Turns out the reason my stuff wasn’t working and I was getting errors in code that was pretty similar to the QuizGame stuff was because all the references to Unity C# stuff wasn’t working in VS. It was giving me squiggles everywhere and since I was doing it from memory and not as part of a tutorial I thought, “I guess I just don’t know what I’m doing yet. So I’ll go ask.”

But it’s pretty much the same as all the other stuff. I just created a new script that attaches to the menu button in the prefab, filled with my method.

VS refused to load the using TMPro or anything else and whenever I tried to use the TextMeshProUGUI it would still give me an error and the .text wouldn’t take, either. I dunno. It was weird. I rebooted it with the instructions at the beginning of the full course for if Intellisense wasn’t working and then tried again after confirming with a friend of mine that I ought to just be able to get that reference to the TMP and then it’s .text.

Instead of having that method in the Question Manager, having it on the button itself was the next step. So that the script already knew that it was the thing the script was attached to. What I didn’t want to have to do was go in and assign a button to a slot for every button. I wanted it to populate itself. And this is how we did it.

Good job! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms