- Home /
UI button keyboard input into typing game ,UI button keyboard to input to Typing Game
Hi, i have followed this tutorial https://www.youtube.com/watch?v=HvMrOoUeqO0&t=627s . I am a little stuck though, I have followed the tutorial and everything works fine but I would like to use this on a mobile phone so I have made a keyboard out of UI buttons and I'm really struggling to get the buttons to input a letter to the word manager script. I am very new to coding so am probably missing something really simple. can anyone help with this?
I'm not about to read the tutorial, but the easiest possible way would be to either create a class attached to each button that contains a character, and a singleton word manager, then make the button pass its character to the word manager on clicking, ideally you would have an active StringBuilder that automatically builds every time it is altered, and refreshes the displayed text.
This is all very simple, and the tutorial you are reading is more than likely complicating things. Please post the code you are using for your buttons and the word manager script, I was going to write an example, but I'm not sure it would be useful for you.
Answer by pgl2389 · Mar 24, 2018 at 11:24 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WordManager : MonoBehaviour {
public List<Word> words;
public WordSpawner wordSpawner;
private bool hasActiveWord;
private Word activeWord;
public void AddWord ()
{
Word word = new Word(WordGenerator.GetRandomWord(), wordSpawner.SpawnWord());
Debug.Log(word.word);
words.Add(word);
}
public void TypeLetter (char letter)
{
if (hasActiveWord)
{
if (activeWord.GetNextLetter() == letter)
{
activeWord.TypeLetter();
}
} else
{
foreach(Word word in words)
{
if (word.GetNextLetter() == letter)
{
activeWord = word;
hasActiveWord = true;
word.TypeLetter();
break;
}
}**strong text**
}
if (hasActiveWord && activeWord.WordTyped())
{
hasActiveWord = false;
words.Remove(activeWord);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WordInput : $$anonymous$$onoBehaviour {
public Word$$anonymous$$anager word$$anonymous$$anager;
void Update () {
foreach (char letter in Input.inputString)
{
word$$anonymous$$anager.TypeLetter(letter);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612155234im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problems with Camera (Camera keeps looking at the feet of my character) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Character getting stuck on curbs.. 0 Answers