I am trying to make a Unity 2D game that the user interacts with using a command-line-style interface. My project contains a Canvas GameObject and a (custom) GameManager GameObject.
The Canvas contains a TMP_Text GameObject for output, and a TMP_InputField for user input.
The GameManager object has three script components attached - GameManager.cs, InputManager.cs, and OutputManager.cs.
My project should be printing out whatever the user types in, until the user types in “exit”.
The problem is that when I press the play button, the Unity Editor freezes, so that I have to force quit it each time this happens.
GameManager.cs looks like this:
using UnityEngine;
public class GameManager : MonoBehaviour
{
private InputManager inputManager;
private OutputManager outputManager;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
inputManager = GetComponent<InputManager>();
outputManager = GetComponent<OutputManager>();
string input = "";
while (input != "exit")
{
input = inputManager.GetContents();
outputManager.Print(input);
}
}
// Update is called once per frame
void Update()
{
}
}
InputManager.cs looks like this:
using System;
using System.Collections;
using System.Linq;
using TMPro;
using UnityEngine;
public class InputManager : MonoBehaviour
{
public TMP_InputField inputField;
private OutputManager outputManager;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
outputManager = gameObject.GetComponent<OutputManager>();
}
// Update is called once per frame
void Update()
{
Focus();
if (Input.GetKeyUp(KeyCode.Return))
{
Focus();
}
}
private void Focus()
{
inputField.Select();
}
public String GetContents()
{
if (Input.GetKeyDown(KeyCode.Return))
{
inputField.text = "";
return inputField.text;
} else
{
return "";
}
}
}
And OutputManager.cs looks like this:
using TMPro;
using UnityEngine;
public class OutputManager : MonoBehaviour
{
public TMP_Text outputField;
public void Println(string output)
{
outputField.text = outputField.text + output + "\n";
}
public void Print(string output)
{
outputField.text = outputField.text + output;
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
I have triple-checked that all public attributes have been assigned in the Inspector.
I’ve also looked at many other similar Unity Forums posts, tried their solutions, and have had no luck in that regard. The OS and Unity installation don’t seem to matter either, because I started to have these problems on a laptop running Ubuntu Linux, and even after reinstalling Unity and trying the project on a different computer running Windows 11, the problems have stayed the same.
Although I am familiar with Java, I’m relatively new to C#, so please explain your responses.
This is my first post here; be tolerant of any guidelines violations that may be present in this post, but please do point them out.
Also, if you see anything I could do that would make my code simpler/more efficient, please mention it!
Thanks a lot!