making a hangman game for my assignment. Its suppose to output the keys that were pressed to the console but nothings appearing. starting at line 47
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
public Text timeField;
public Text wordToFindField;
private float time;
private string[] wordsLocal = {"GRASS", "CHOCOLATE", "HOT", "CHEESE","CONSOLE" };
private string chosenWord;
private string hiddenWord;
// Start is called before the first frame update
void Start()
{
Debug.Log("Hello");
chosenWord = wordsLocal[Random.Range(0, wordsLocal.Length)];
for (int i = 0; i < chosenWord.Length; i++)
{
char letter = chosenWord[i];
if(char.IsWhiteSpace(letter))
{
hiddenWord += " ";
}
else
{
hiddenWord +="_";
}
}
wordToFindField.text = hiddenWord;
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
timeField.text = time.ToString();
}
private void OnGui()
{
Event e = Event.current;
if(e.type == EventType.KeyDown && e.keyCode.ToString().Length == 1)
{
string pressedLetter = e.keyCode.ToString();
Debug.Log("Keydown event was triggered" + pressedLetter);
if (chosenWord.Contains(pressedLetter))
{
// Grass
int i = chosenWord.IndexOf(pressedLetter);
while(i != -1)
{
hiddenWord = hiddenWord.Substring(0, i) + pressedLetter + hiddenWord.Substring(i + 1);
Debug.Log(hiddenWord);
chosenWord = chosenWord.Substring(0, i) + "_" + chosenWord.Substring(i + 1);
i = chosenWord.IndexOf(pressedLetter);
}
wordToFindField.text = hiddenWord;
}
else
{
}
}
}
}