0

I am making this sum creator where user will have to type an answer using custom keyboard. and on check button click if answer is correct then new question is loaded.

My problem is after answering first question answer button reset to blank but when user types next answer, only one last alphabet is deleted (for example 5 from 15). and when i type 14 it shows 114 (1 from previously typed answer).

I need help to reset answer button text to blank.

I am using buttons because later i want to add more questions at the same time so user will have multiple answers to click and type.

Can anyone please help me on this? Also tell me if this is the right method to achieve what i want.

I am calling backspace function to delete previous answer and also setting text to blank.

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

public class Keyboard : MonoBehaviour
{
    string word = null;
    int wordIndex = -1;
    string alpha = null;
    string alpha2 = null;
    public Text userAnswer1 = null;
    public Text valueA, valueB;
    public Text scoreCount;
    private int a, b, answer1, score;
    char[] nameChar = new char[5];


    private void Start()
    {          
        SumCreator();
    }

    public void nameFunc (string alphabet)
    {
        wordIndex++;
        char[] keepchar = alphabet.ToCharArray();
        nameChar[wordIndex] = keepchar[0];
        alpha = nameChar[wordIndex].ToString();
        word = word + alpha;
        userAnswer1.text = word;      
    }

    public void BackspaceFunction()
    {
        if (wordIndex >= 0)
        {
            wordIndex--;

            alpha2 = null;
            for (int i = 0; i < wordIndex + 1; i++)
            {
                alpha2 = alpha2 + nameChar[i].ToString();
            }
            word = alpha2;
            userAnswer1.text = word;
        }
    }

    public void SumCreator ()
    {
        a = Random.Range(0,15);
        b = Random.Range(0,15);
        answer1 = a + b;
        valueA.text = a.ToString();
        valueB.text = b.ToString();
        scoreCount.text = "score " + score.ToString();
    }

    public void CheckAnswer()
    {
        Text buttonText = userAnswer1.GetComponentInChildren<Text>();

        if (answer1 == int.Parse(userAnswer1.text))
        {
            score++;
            // userAnswer1.text = string.Empty;
            buttonText.text = string.Empty;
        }  

        SumCreator();
    }
}
Sarita
  • 51
  • 3
  • 8
  • Possible duplicate of [C# Unity - How can I clear an InputField](https://stackoverflow.com/questions/37754617/c-sharp-unity-how-can-i-clear-an-inputfield) – derHugo Jan 22 '19 at 12:56

2 Answers2

1

I've edited my answer and removed the now irrelevant parts.

Once the button "Check" is clicked, first of all erase the text in the result textbox, then do the whole other logic. To erase the text you can use next piece of code:

Text buttonText = buttonName.GetComponentInChildren<Text>();
buttonText.text = string.Empty;

You probably want to have this "buttonText" property as a global and get it once, at the start of the program instead of getting it every time the button is clicked. It won't do much difference in a small scale program, but it's a right way of thinking.

After checking your code a bit more, I can summarize your problem:

The whole logic of your program is flawed, there're many unnecessary complicated things which make it fail in several places. It is understandable, everybody goes through this stage, nothing to be ashamed or worried about. Either way it's my subjective opinion, which may be wrong.

Back to your code, all you have to do is update your result text, say "txtResult", once anything happens.

Once you click a number, do "txtResult += numberClicked".

Once you click backspace, remove last char of txtResult. Here is a question with many answers on how to do it, it's really simple.

Once you click "Check", in case it's the right number, set txtResult to empty.

Also, every time you update txtResult, you're supposed to update the UI too of course. Let's say you do it every time, it would be one line to update txtResult, and one line to update UI for each of the above 3 cases. So in total 6 lines. A check for an empty string while in "Backspace" function adds another line. My math could be wrong, but either way, it's quite short and simple approach, nothing too complicated.

You just lack relevant knowledge, otherwise you wouldn't be doing that nightmare in your Backspace function.

Regarding the "nameFunc" function, the whole 6 lines could be replaced with "txtResult += alphabet", isn't it? I'm not sure what you get in alphabet parameter, but either way, string is an array of chars, so you can also do "txtResult += alphabet[0]" instead of what you have there.

So, in total, you got it all right, the logic was right, you figured the main aspects. But you over complicated the whole thing. I believe you'll be fine after reading all this text, and wish you the best.

MaxB
  • 438
  • 6
  • 15
  • i tried what you suggested but it does not erase anything. still adding new answer to previous one – Sarita Jan 22 '19 at 09:59
  • so i added above code in start function and setting string.empty in check but still not working. – Sarita Jan 22 '19 at 10:15
  • It looks like you've made many changes along the way of writing this program, and tried different things and now it just became a bit messy. I'd suggest you to go step by step all over your code and check the logic. Either way, after you fix the line from my Edit2, you probably won't see any answer ever, so you should move it to the beginning of the "CheckAnswer" function. – MaxB Jan 22 '19 at 10:27
  • ok i will go step by step. i have named that button as "answer" in the hierarchy – Sarita Jan 22 '19 at 10:45
  • i have edited my code int he question now. no other changes except check function. – Sarita Jan 22 '19 at 10:56
  • @Sarita what is nameFunc for? When does it happen? When user clicks on a number to enter in answer? – MaxB Jan 22 '19 at 11:54
  • yes nameFunc displays string value that is assigned to each keypad button to enter answer – Sarita Jan 22 '19 at 12:02
  • @Sarita So, if I understand right, this function runs every time you click a number? You have this "word" there, which you put as a text in answer. But, notice that you never reset its value. – MaxB Jan 22 '19 at 13:19
  • i tried 1 word = string.Empty;` or ` word = null;` but when i use backspace button it shows previously entered values too – Sarita Jan 22 '19 at 14:09
  • @Sarita Inside your Backspace function you have "word = alpha2;" which sets the word depending on "wordIndex". Your whole program is unnecessary complicated. I don't want to get into the logic behind your use of "wordIndex". I can suggest you another approach for "Backspace" function. In order to remove last char in the result text, you can use String class and its functions. Google "Delete last char of string c#", there's a good StackOverflow question with this title – MaxB Jan 22 '19 at 16:55
  • thank you so much for explaining the whole thing. yes i am just a beginner and trying to learn unity using online resources. I will redo the whole thing as you have guided. thanks s lot – Sarita Jan 23 '19 at 05:06
  • I followed exact steps as you have written and got the desired output. – Sarita Jan 23 '19 at 06:53
  • @Sarita I'm glad to hear it! Good luck and have a nice day! – MaxB Jan 23 '19 at 07:12
0

If you want to clear your Text object when you have succesfully entered your answer, you should not call your "BackSpace" function.

Just replace your code to this:

if (answer1 == int.Parse(userAnswer1.text))
{
    score++;
    userAnswer1.text = string.Empty;

This will clear the text element.

You could also look into using InputFields in Unity, which are designed for entering input and automatically support backspace and other keyboard functions. If you do, make sure that you set the InputField's ContentType to either Integer Number or Decimal Number

Immorality
  • 2,164
  • 2
  • 12
  • 24
  • code that you provided is loading next question with blank button but when i type new answer same thing is happeninig. new answer is added to previous one – Sarita Jan 22 '19 at 10:03