简体   繁体   English

Unity - 用户按下特定对象(按钮),这反过来增加或减少他们的分数

[英]Unity - User presses a specific object (a button) which in turn increases or decreases their score

I'm new to Unity and what I'm trying to create is a quiz.我是 Unity 的新手,我正在尝试创建一个测验。 What I want is the user to click on an answer (Button Object) and if its right, increase the score.我想要的是用户单击答案(按钮对象),如果正确,则增加分数。 And if its wrong, decrease the score.如果错了,降低分数。 However, I cant seem to find the code to increase and decrease the score.但是,我似乎无法找到增加和减少分数的代码。 Any help with this would be highly appreciated.对此的任何帮助将不胜感激。 I have a score manager script with a starting score of 10 if that's any help.如果有帮助的话,我有一个分数管理器脚本,起始分数为 10。 The script is following:脚本如下:

记分管理器脚本

You need some reference to check against if a given answer is correct.您需要一些参考来检查给定的答案是否正确。 Lets assume you have a question and four answers (a, b, c, d).假设您有一个问题和四个答案(a、b、c、d)。
You can give a button (the unity buttons under UI) a target method to execute on click and specify a value eg a string stating a, b, c or d.您可以为按钮(UI 下的统一按钮)提供一个在单击时执行的目标方法并指定一个值,例如表示 a、b、c 或 d 的字符串。 Therefor you need an object which has a script with that method.因此,您需要一个具有该方法的脚本的对象。

I recommend having a look at this tutorial video for the button on how to set this up: https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button我建议看一下这个教程视频,了解如何设置按钮: https : //unity3d.com/learn/tutorials/modules/beginner/ui/ui-button

In that method you then check if the answer was correct, eg you can have a Dictionary<string, string> where the first string corresponds to the question and the second one to the correct answer and if the value from the button matches this answer, the user picked the correct answer.在该方法中,您然后检查答案是否正确,例如您可以有一个Dictionary<string, string> ,其中第一个字符串对应于问题,第二个字符串对应于正确答案,并且按钮中的值是否与此答案匹配,用户选择了正确的答案。

For further help we would need to know how you have your questions set up, how you know which is the current question, which is the correct question to that answer...如需进一步帮助,我们需要知道您如何设置问题,您如何知道哪个是当前问题,哪个是该答案的正确问题......

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

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