Unity Hangs/Freezes on Play Button Press

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!

Two things to note… one is simply that triple-checking the Inspector is somewhat useful but a more effective way is to output Debug.Log statements that confirm what you believe the values to be. It is surely rare but not impossible for something in the Inspector to “change” as the app runs.

In GameManager.Start for instance confirm and inputManager and outputManager are not null. They won’t be but again this is how you should confirm runtime assignments.

Now to your problem. I’m going to guess it is due to your while loop in Start. As the comment says it is called once. It isn’t intended to be the main game loop. Place your input logic in Update which runs continuously until you exit.

1 Like

Thanks a lot; I’ll test this out when I have time and post the results here.

It worked!
Thanks a lot!
I moved the contents of the while loop to Update() and everything was fixed.