I know that in “normal” C# you can use Console.WriteLine("Message") to write something to a console, and then read it with ReadLine, but is it possible to tie this into my game?
What I mean is that I want to print some text to the console, read what the player typed in and then load a level based on what the player typed (if the player types in “555” it will load level 555), and after the level is completed it comes back to the console.
Any ideas?
Easiest is probably to make a UI that has a text entry field, get the input from there and use it.
4 Likes
Hello That_Martin_Guy!
As Kurt-Dekker said, the easiest solution would be to create an Input Field in your scene (GameObject > UI > Input Field), then recuperate the input from the user.
It could look like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class getInputField : MonoBehaviour
{
private string playerInput; //Used to store the input from the player
void Update ()
{
if (Input.GetKeyDown(KeyCode.A)) //The condition goes here (when you want to call the function)
getTextFromInput ();
}
private void getTextFromInput ()
{
playerInput = GetComponent<InputField>().text; //Storing what the player typed into playerInput
Debug.Log (playerInput); //Simple test to see if you actually get the correct input
}
}
Then, you would need to attach this script to the InputField game object in your Hierarchy (it’s possible to attach this script to any game object you want, but you’d need to change the code above a bit).
Now that you have what the player typed stored in a string (playerInput), you may use it as you wish! 
1 Like