I’m trying to make a sort of developer’s console thing that lets me add values while in-game and stuff. I can get it to open, but once it does open it just creates an endless stream of spaces and zeroes.
Additionally, I can only open it with GetButton because it won’t open at all if I use GetButtonDown.
Here’s the script:
using UnityEngine;
using System.Collections;
public class DevConsole : MonoBehaviour {
private bool IsConsoleOpen;
string Cmd1, Cmd2;
string CommandInput (string Command, int AddedValue)
{
return string.Format ("{0} {1}", Command, AddedValue);
}
public string InputCmd;
public int CmdVal;
// Use this for initialization
void Start () {
Cmd1 = "addrings";
Cmd2 = "addlives";
IsConsoleOpen = false;
}
// Update is called once per frame
void Update () {
if (Input.GetButton ("Open Console") && !IsConsoleOpen)
{
IsConsoleOpen = true;
Time.timeScale = 0;
}
else if (Input.GetButton ("Open Console") && IsConsoleOpen)
{
IsConsoleOpen = false;
Time.timeScale = 1;
}
}
void OnGUI()
{
if (IsConsoleOpen)
{
InputCmd = GUI.TextField (new Rect (Screen.width / 2 - 200, Screen.height - 150, 400, 30), CommandInput (InputCmd, CmdVal));
if (GUI.Button (new Rect (Screen.width / 2 - 35, Screen.height - 115, 70, 30), "Submit"))
{
if (InputCmd == Cmd1)
{
gameObject.GetComponent<StockManager>().TotalRings += CmdVal;
}
else if (InputCmd == Cmd2)
{
gameObject.GetComponent<StockManager>().TotalStock += CmdVal;
}
else if (InputCmd != Cmd1 && InputCmd != Cmd2)
{
IsConsoleOpen = false;
}
}
}
}
}