Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Hangman : MonoBehaviour
- {
- public string[] words;
- private int lifes = 10;
- private string userInput = "";
- private string guessedword = "";
- public string wordChosen = "";
- private void Start()
- {
- wordChosen = words[Random.Range(0, words.Length)];
- guessedword = new string("-"[0] , wordChosen.Length);
- }
- private void OnGui()
- {
- if (lifes > 0 && guessedword != wordChosen)
- {
- GUILayout.Box(guessedword);
- userInput = GUILayout.TextField(userInput);
- if(GUILayout.Button("Try"))
- {
- if (userInput.Length > 1)
- print("Please Enter One Character At A Time :)");
- else
- checkChar(userInput[0]);
- }
- }
- else
- {
- if (lifes > 0)
- GUILayout.Box("Congratulations! You Win!");
- else
- GUILayout.Box(" Awww You Lose :(");
- }
- }
- private void CheckChar(char c)
- {
- bool charExists = false;
- for(int x = 0; x = wordChosen.length; x++)
- {
- if (wordChosen[x] == c)
- {
- charExists = true;
- string temp = guessedWord.Substring(0 , x);
- guessedWord = temp + c.ToString() + guessedWord.Substring(x + 1, guessedWord.Length - x - 1);
- }
- }
- if(!charExists)
- lifes --;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement