简体   繁体   English

问答游戏 Unity

[英]Quiz Game Unity

I am trying to make a simple quiz game using Unity3D and Visual Studio 2019 and I've been having trouble with these code errors.我正在尝试使用 Unity3D 和 Visual Studio 2019 制作一个简单的问答游戏,但我遇到了这些代码错误的问题。 I can't quite seem to figure out where I am going wrong.我似乎无法弄清楚我哪里出错了。 Attached is my code and a picture of the code errors, any help would be much appreciated.附件是我的代码和代码错误的图片,任何帮助将不胜感激。

This is the "Using part", I've stated "System"这是“使用部分”,我已经说明了“系统”

Using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

This part is from a tutorial that I was following, it worked on the "windows forms apps, visual studio" but I need to convert it to unity and that is where I am having difficulty这部分来自我正在关注的教程,它适用于“windows forms apps,visual studio”,但我需要将其转换为 unity,这就是我遇到困难的地方

namespace Quiz_game
{
    public partial class Quiz
    {
        public GameObject Question;
        public GameObject Option01;
        public GameObject Option02;
        public GameObject Option03;
        public GameObject Option04;
        public int CorrectAnswer;
        private int questionNumber;
        private int score;
        private int percentage;
        private int totalQuestions;

        private void checkAnswerEvent(object sender, EventArgs e)
        {
            var senderObject = (Button)sender;
            int buttontag = Convert.ToInt32(senderObject.Tag);

            if (buttontag == CorrectAnswer)
            {
                score++;
            }
            if (questionNumber == totalQuestions)
            {
                //work out the percentage
                percentage = (int)Math.Round((double)(score * 100) / totalQuestions);


                MessageBox.Show(
                  "Quiz Ended" + Environment.NewLine + "You have answered " + score + " question(s) correctly" + Environment.NewLine + "Your total percentage is " + percentage + "%" + Environment.NewLine + "Click OK to play again"
                  );
                score = 0;
                questionNumber = 0;
                askQuestion(questionNumber);
            }
            questionNumber++;
            askQuestion(questionNumber);
        }


        private void askQuestion(int qnum)
        {
            switch (qnum)
            {
                case 1:
                    Question.Text = "What does solution mean?";
                    Option01.Text = "a homogenous mixture that is used for oral, parental, or external purposes";
                    Option02.Text = "blue";
                    Option03.Text = "orange";
                    Option04.Text = "yellow";
                    CorrectAnswer = 1;
                    break;
            }
        }
    }
}

代码错误

  • MessageBox is part of System.Windows.Forms - I don't see you using that namespace nor do I think it is supported within Unity (might be wrong on the second part though) and/or at least shouldn't be used. MessageBoxSystem.Windows.Forms一部分 - 我没有看到您使用该命名空间,我也不认为它在 Unity 中受支持(尽管第二部分可能是错误的)和/或至少不应该使用。

    Rather build your own dialogue pop-up using the Unity UI System.而是使用 Unity UI 系统构建您自己的对话框弹出窗口。

  • UnityEngine.UI.Button has indeed no Tag . UnityEngine.UI.Button确实没有Tag You probably rather mean tag .你可能更想说tag

    However, this is a string like eg "SomeTag" and connot simply be converted into an int using Convert.ToInt32 ... so instead you should use the string and do但是,这是一个像"SomeTag"这样的string并且不能简单地使用Convert.ToInt32将其转换为int ......所以你应该使用该字符串并执行

    public string CorrectAnswerTag;

    and

    if(senderObject.CompareTag(CorrectAnswerTag))
  • And well no, GameObject doesn't have any Text ... you probably rather would want to make your fields have the correct types好吧,不, GameObject没有任何Text ……您可能更希望让您的字段具有正确的类型

    public Text Question; public Text Option1; public Text Option2; public Text Option3; public Text Option4;

I suggest you make your way through Unity Learn - UI Components我建议你学习 Unity Learn - UI Components

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM