What does question.GetQeustion(); do exactly?

Hello,

I am not sure I really understand what this code does.

So I have a variable called question of type QuestionSO defined in my quiz.cz

With the method GetQuestion() I access the QuestionSO.cs and return the question variable of QuestionSO.cs which is of type string (basically the question text defined in QuestionSO.cs).

Does it then store the string of the question variable of QuestionSO.cs in the question variable of Quiz.cs?

I mean I know that we access/change the text in the text field of the TMPro object with questionText.text but the right side of the “equation” confuses me.

Thanks

Hi,

You are right. We call GetQuestion() on a QuestionSO object (not the script!), and that method returns something: a string. Since the computer cannot remember anything, we need to assign the returned string value to a variable, for example, questionText.text. Otherwise, the string gets lost, and we would have to call the GetQuestion() again.

If you need a semi-real world example: The postman has a letter for you but since you do not have a postbox, he simply throws it into the dustbin. You call the post office asking where your letter is, and they send you another one which ends up in the dustbin again. Of course, you are not aware that this is happening, so you are waiting for the postman to complain. He has no clue what you are talking about and gives you the desired letter. However, whenever you are not waiting for the postman to receive the letter personally, you do not receive any letter. After some research on the internet, you learn that you need to install a postbox to receive letters without meeting the postman personally. You do that, and lo and behold, you suddenly receive letters as expected.

The same always happens in your C# code, not just in this specific game.

In C#, the dustbin is the so-called garbage collector. All data that do not have a place get destroyed. When receiving the data package, you need to decide immediately whether you process it directly or whether you assign it to a variable for later use. If you don’t do anything, the garbage collector takes care of it.

Hopefully, this cleared it up for you. :slight_smile:


See also: