destroying objects when answering a question correctly
Hi guys, Is there any way that you can allow users to answer a question correctly before they can collect a coin?
I'm not good at coding but i tried this
var x =2;
var y =3;
var z= 4;
var HOB =25;
var Sum = x+y;
function OnTriggerEnter(info: Collider){
GUI.Label(Rect((Screen.width)/2-230, 130,200,50),"what is the sum of x+y?");
GUI.textfield(Rect((Screen.Width)/2-20,130,200,HOB),45);
if(GUI.button(Rect((Screen.width)/2-230,420,200,HOB),"OK!"))
{
if(Sum !=(x+y)){
Debug.Log("wrong answer, please try again");
}else{ Destroy(gameObject);
CoinSystem.coinscollect +=1;
}
}
}
Answer by Cepheid · Nov 07, 2015 at 09:20 PM
Hi. @Rissa_Ja There are a couple things wrong with the code but overall you're pretty much there. Below is a working example of the code with revision and some quick comments detailing them.
#pragma strict
var x : int = 2;
var y : int = 3;
//The total sum of x+y which will be compared against the answer.
var answer : int = x+y;
var z : int = 4;
var HOB : int = 25;
//The string which the player will type and will be compared.
var sum : String = null;
//Boolean to chech whether or not the question should be displayed.
var shouldDisplayQuestion : boolean = false;
function OnTriggerEnter (other : Collider)
{
//Player has entered collider and we want to display
//the question so we turn our boolean to true.
shouldDisplayQuestion = true;
}
//Function which handles GUI output.
function OnGUI ()
{
//An if statement to check for whether or not we should display the question.
if(shouldDisplayQuestion)
{
//If true we then run the display question method.
DisplayQuestion();
}
}
//Function which handles the question.
function DisplayQuestion ()
{
GUI.Label(Rect((Screen.width)/2-230, 130, 200, 50), "What is the sum of x+y?");
//The string we wish to input with our text field.
sum = GUI.TextField(Rect((Screen.width)/2-20,130,200,HOB), sum, 45);
//Check for whether the button was pressed.
if(GUI.Button(Rect((Screen.width)/2-230,420,200,HOB),"OK!"))
{
//If statement which compares the players string answer
//with that of x+y's answer parsed into a string.
if(sum != answer.ToString())
{
Debug.Log("wrong answer, please try again");
}
else
{
Destroy(gameObject);
CoinSystem.coinscollect +=1;
//We turn our boolean to false as the question as has been answered
//and we no longer want to show the menu.
shouldDisplayQuestion = false;
}
}
}
Now for a bit more information on the code the main issues with your code are:
Your GUI statements must be declared within or from the OnGUI function otherwise they will not be drawn and the console will throw an error.
The text field takes a string value and as such can only compare the x+y question as a string value so you have to parse your int's to a string to compare them.
To deal with the OnGUI function i've used a boolean to decide whether or not the OnGUI function should display the question based on whether the player has entered the trigger.
The text field requires a string value to change in this case i have used the sum variable.
That's pretty much it, if you need any more help or this code is causing problems don't hesitate to ask. I hope this helps! :)
@Cepheid thanks alot! it worked. the only problem i had was upon answering incorrectly, it did not show "wrong answer, please try again".
on another note i now realize i could not exit the trigger when the answer was wrong lol. i guess i have to use "on trigger exit" within the code or give users a chance to try again. i'll try writing it in a code and if it does not work, i'll get back to you.
thanks again! :D
hi @Cepheid i posted another question about getting the question to display when my timer reaches 20seconds. but since you helped me with the first code, i decided to ask you here. do you see where i went wrong? additionally is there anyway to store the time a player finished a game level into a localhost db(xampp)? Thanks in advance :)
pragma strict
import UnityEngine.UI;
var counterText : UI.Text;
var $$anonymous$$utes : float;
var seconds : float;
var x : int = 8;
var y : int = 4;
var answer : int = x-y;
var HOB : int = 25;
var sum : String = null;
var shouldDisplayQuestion : boolean = false;
function Start () {
counterText = GetComponent.<Text>()as Text;
}
function Update () {
$$anonymous$$utes = $$anonymous$$athf.Floor(Time.time / 60f);
seconds = $$anonymous$$athf.Floor(Time.time % 60f);
counterText.text = $$anonymous$$utes.ToString ("00") + ":" + seconds.ToString ("00");
}
function on$$anonymous$$inuteCheck()
{
if(Time.time >= 20) // This isn't working
{
shouldDisplayQuestion = true;
}
}
function OnGUI ()
{
if(shouldDisplayQuestion)
{
DisplayQuestion();
}
}
function DisplayQuestion ()
{ GUI.Label(Rect((Screen.width)/2-230, 130, 200, 50), "What is 8-4?");
sum = GUI.TextField(Rect((Screen.width)/2-20,130,200,HOB), sum, 45);
if(GUI.Button(Rect((Screen.width)/2-230,420,200,HOB),"O$$anonymous$$!"))
{
if(sum != answer.ToString())
{
print("wrong answer, please try again");
}
else
{
CoinSystem.coinscollect +=100;
Score.gscore+=100;
shouldDisplayQuestion = false;
}
}
if(GUI.Button(Rect((Screen.width)/1-230,320,100,HOB),"Exit!"))
{
shouldDisplayQuestion = false;
}
}