I spent way too long trying to get my Number Wizard to pick a different answer text from a string of possibles. I don’t fully understand what I’ve done but it did force me to use the Unity documentation and forums!
Great to see everyone’s Number Wizards here as well.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class ArrayExtensions
{
public static T RandomItem<T>(this T[] array)
{
return array[Random.Range(0, array.Length)];
}
}
public class NumberWizard : MonoBehaviour
{
int max = 1000;
int min = 1;
int guess = 500;
// Start is called before the first frame update
void Start()
{
Debug.Log("I am the Number Wizard! I've been expecting you.");
Debug.Log("You must think of a number between " + min + " and " + max);
Debug.Log("Don't tell me what it is! I will guess.");
Debug.Log("Use the UP ARROW to tell me if your number is HIGHER than my guess");
Debug.Log("Use the DOWN ARROW to tell me if your number is LOWER than my guess");
Debug.Log("Use the ENTER key to tell me if I am CORRECT!");
Debug.Log("Is your number " + guess + "?");
max = max + 1;
}
// Update is called once per frame
void Update()
{
string[] wizardAnswer = { "Is it ", "How about ", "Hmmm, ", "Ooh I'm close, I can just tell! What about " };
if (Input.GetKeyDown(KeyCode.UpArrow))
{
min = guess;
guess = (max + min) / 2;
{
Debug.Log(wizardAnswer.RandomItem() + guess + "?");
}
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
max = guess;
guess = (max + min) / 2;
Debug.Log(wizardAnswer.RandomItem() + guess + "?");
}
else if (Input.GetKeyDown(KeyCode.Return))
{
Debug.Log("AHA! I knew it!");
Debug.Log("I am so clever.");
Debug.Log("Goodbye!");
}
}
}