오늘은 정답보기를 클릭하면 정답 확인이 되는 코드를 작성해봅니다.

GameScore 를 만들고 텍스트 게임 오브젝트에 게임 스코어를 할당해봅니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public Text QuizText;
public Text RightAnswer;
public Text ButtonText1;
public Text ButtonText2;
public Text ButtonText3;
List<string> QuizTextArray = new List<string>();
List<string> AnswerTextArray = new List<string>();
public string[] answer;
public int GameScore = 0;
void Awake()
{
instance = this;
}
void Start()
{
QuizTextArray.Add("3+2=");
QuizTextArray.Add("5+4=");
QuizTextArray.Add("7+3=");
AnswerTextArray.Add("5,6,7,5");
AnswerTextArray.Add("8,9,10,9");
AnswerTextArray.Add("9,10,11,10");
string AnswerText = AnswerTextArray[0];
answer = AnswerText.Split(',');
QuizText.text = QuizTextArray[0];
ButtonText1.text = answer[0];
ButtonText2.text = answer[1];
ButtonText3.text = answer[2];
RightAnswer.text = answer[3];
}
}

버튼을 클릭할 때 텍스트 게임오브젝트의 값과 선택한 보기의 값을 확인합니다. 정답일 경우 출력하도록 합니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonCheck : MonoBehaviour
{
public void AnswerButton()
{
Debug.Log("보기: " + this.GetComponentInChildren<Text>().text);
Debug.Log("정답: " + GameObject.FindGameObjectWithTag("RightAnswerText").GetComponent<Text>().text);
Debug.Log("정답: "+ GameManager.instance.answer[3]);
Debug.Log("정답: " + GameManager.instance.GameScore);
if (GameObject.FindGameObjectWithTag("RightAnswerText").GetComponent<Text>().text == this.GetComponentInChildren<Text>().text)
{
Debug.Log("정답입니다~");
}
}
}

다음과 같이 정답 보기를 클릭할 경우 “정답입니다“ 라는 문장이 출력됩니다~~

오늘은 정답보기를 클릭하면 정답 확인이 되는 코드를 작성해봅니다.
GameScore 를 만들고 텍스트 게임 오브젝트에 게임 스코어를 할당해봅니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public Text QuizText;
public Text RightAnswer;
public Text ButtonText1;
public Text ButtonText2;
public Text ButtonText3;
List<string> QuizTextArray = new List<string>();
List<string> AnswerTextArray = new List<string>();
public string[] answer;
public int GameScore = 0;
void Awake()
{
instance = this;
}
void Start()
{
QuizTextArray.Add("3+2=");
QuizTextArray.Add("5+4=");
QuizTextArray.Add("7+3=");
AnswerTextArray.Add("5,6,7,5");
AnswerTextArray.Add("8,9,10,9");
AnswerTextArray.Add("9,10,11,10");
string AnswerText = AnswerTextArray[0];
answer = AnswerText.Split(',');
QuizText.text = QuizTextArray[0];
ButtonText1.text = answer[0];
ButtonText2.text = answer[1];
ButtonText3.text = answer[2];
RightAnswer.text = answer[3];
}
}
버튼을 클릭할 때 텍스트 게임오브젝트의 값과 선택한 보기의 값을 확인합니다. 정답일 경우 출력하도록 합니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonCheck : MonoBehaviour
{
public void AnswerButton()
{
Debug.Log("보기: " + this.GetComponentInChildren<Text>().text);
Debug.Log("정답: " + GameObject.FindGameObjectWithTag("RightAnswerText").GetComponent<Text>().text);
Debug.Log("정답: "+ GameManager.instance.answer[3]);
Debug.Log("정답: " + GameManager.instance.GameScore);
if (GameObject.FindGameObjectWithTag("RightAnswerText").GetComponent<Text>().text == this.GetComponentInChildren<Text>().text)
{
Debug.Log("정답입니다~");
}
}
}
다음과 같이 정답 보기를 클릭할 경우 “정답입니다“ 라는 문장이 출력됩니다~~