First Answer Button text comes from Question 1 Element 0 s text

Hello i hope you can help me
you will see in the pictures, in game first anwer needs show “a” answer but in game answer button texts, takes data from Question 1 Element 0 text, if i write Answer button “a”, “m” to Element 0, it changes answer button text to “m”, in game it should show “a” answer but it shows “m” answer from Element 0, in game both of are changes, other answer buttons working well.
I hope I explained well
Thanks for the answers



These are my codes

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class Quiz : MonoBehaviour
{
    [SerializeField] TextMeshProUGUI questionText;
    [SerializeField] QuestionSO question;
    [SerializeField] GameObject[] answerButtons;
    
    void Start()
    {
        questionText.text = question.GetQuestion();
        for (int i = 0; i < answerButtons.Length; i++)
        {
            TextMeshProUGUI buttonText = answerButtons[0].GetComponentInChildren<TextMeshProUGUI>();
            buttonText.text = question.GetAnswer(0);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "Quiz Question", fileName = "New Question")]
public class QuestionSO :  ScriptableObject
{
    [TextArea(2,6)]
    [SerializeField] string question = "Enter new question text here";
    [SerializeField] string[] answers = new string[4];
    [SerializeField] int correctAnswerIndex;

    public string GetQuestion()
    {
        return question;
    }

    public string GetAnswer(int index)
    {
        return answers[index];
    }
    
    public int GetCorrectAnswerIndex()
    {
        return correctAnswerIndex;
    }
}

Hi,

In C#, arrays start at index 0, hence the first element in the Unity Editor is labelled with Element 0 instead of Element 1. I do not see any problem in your screenshots.

Why do you expect the game to show an “a” if your game uses the data from the “Question 1” object?

im not sure what is wrong, but first answer comes from Element 0, others comes from Answer Button 1 2 3. i dont know which one of it not working, but problem is answers comes from diffrent places, all answers should either comes from Element 0 1 2 3 or Answer Button 0 1 2 3. maybe my code is wrong? maybe someone could show correct codes it can help

Here. You loop through everything, but always set element 0. It should probably be

    void Start()
    {
        questionText.text = question.GetQuestion();
        for (int i = 0; i < answerButtons.Length; i++)
        {
            TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();
            buttonText.text = question.GetAnswer(i);
        }
    }

it worked that way, i do same with video 2d unity course, maybe video wrong ? im not sure
Thank you

No. The video is not wrong.

This is the code in the repository

And this is the code in the video

But don’t beat yourself up. It’s easy to make mistakes. That’s how we learn

video first showin wrong codes, than turn correct codes, thats confuses us, im not english speaker, its hard to find that moments when the codes are correct, mayse end of the videos they can show all the correct codes for that lesson or put in the soruces

I’ve just rewatched the video, and I’m not quite sure what you mean.

As of the 2:25 mark, Gary explains how to access the first element in the array. In C#, arrays, Lists and other collections start at index 0. This means: The first element is always at index 0.

Then he duplicates the code, so we are able to access the other buttons and answers as well.

However, the code looks basically the same. Just the numbers are different. For this reason, Gary refactors the code. He first explains what a for-loop is. He adds a for-loop. Then, as of the 8:30 mark, he replaces the hard-coded 0 with i. The complete script can be seen at 8:44, which is basically at the end of the video.

As bixarrio wrote, the complete code can also be found on GitLab. There is a link in the resources of the video. If you are in doubt, always check the repository. I do the same because reading code in videos is difficult, and it is easy to miss a detail.

If you noticed a mistake in the video, please let us know so Gary can look into it and fix it. :slight_smile:


See also:

when i say wrong codes that i mean codes are not final version end of the video, little translate mistakes
i can see now what you mean, im not english speaker that is why im struggling
its hard cath those moments when the final version of codes on the screen
now i can see final codes in the sources
maybe ? end of the video shows us before and after codes, what changes in that video, can be better?
because i like to writes myself from the video and understand, just my idea
thank you for all answers, its much easier now
sorry for the troubles, my mistakes

Well, that’s difficult even for native speakers. We are no clairvoyants and cannot know if there will be more in the video. Only the instructor knows. We have to watch the video first. Then we know.

Nevertheless, I’m forwarding your suggestion to Gary. Maybe he’ll agree with you and will include a before-after at the end of his future videos. :slight_smile:

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