Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Rissa_Ja · Nov 07, 2015 at 03:41 AM · destroy object

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;
 }
 
 
 }
 }



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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! :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rissa_Ja · Nov 08, 2015 at 04:03 AM 0
Share

@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

avatar image Rissa_Ja · Nov 14, 2015 at 06:12 AM 0
Share

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;
   
   }

}

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Help with coin pick up script 1 Answer

Problem after 5 seconds destroy/deactivate component 1 Answer

How to destroy the most ancient clone? 0 Answers

Who collided first 0 Answers

Destroy instatiate object on trigger enter / collision,destroy instantiate prefab on trigger enter 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges