Why am I unable to type into the text field?
Hi there everyone, I am having a problem with my current code for a Journal game object. the object is a journal which allows players to type into it whatever they like, I recently added the lines which stop the player from moving otherwise they just keep moving around while typing, the player stands still now but can't type in the box anymore. Below is my code, would gladly appreciate any help i can get c:
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using TMPro; using System;
public class JournalManager : MonoBehaviour { //This enum is used to have a setting //of whether to check if the gameobject is //active locally or active on scene public enum SetActiveType { activeSelf, activeInHierarchy }
[Header("General Settings")]
[Tooltip("Use a unique ID for different journals! Will be saved to disk with this name")]
public string journalID = "Journal_0";
[Tooltip("This key will be used to enable the journal")]
public KeyCode toggleJournalKey = KeyCode.Tab;
[Space(10)]
[Header("Additional Settings")]
public GameObject journalGameObject;
public TMP_InputField inputField;
public SetActiveType setActiveType = SetActiveType.activeInHierarchy;
public GameObject firstPersonController;
private bool m_inputBlocked;
private void Start()
{
SceneManager.activeSceneChanged += OnSceneLoaded;
m_inputBlocked = false;
//Load journal on game start
LoadJournal();
}
private void OnSceneLoaded(Scene arg0, Scene arg1)
{
//When a new scene is loaded we want to save the journal
SaveJournal();
}
private void OnApplicationQuit()
{
//When the application quits we want to save the journal
SaveJournal();
}
/// <summary>
/// Set to true to disable journal and block further input
/// Set to false to enable input
/// Use Case: Cutscenes
/// </summary>
/// <param name="blockInput"></param>
public void BlockInput(bool blockInput)
{
//this is used to block input
//use it if you need to add cutscenes or something
m_inputBlocked = blockInput;
if(blockInput)
{
journalGameObject.SetActive(false);
}
}
void Update()
{
//check whether or not the input is blocked atm
if (!m_inputBlocked)
{
if (Input.GetKeyDown(toggleJournalKey))
{
ToggleJournal();
}
}
}
/// <summary>
/// Use this function to toggle journal on or off
/// Made public so you can use this in conjunction with unity events
/// </summary>
public void ToggleJournal()
{
switch (setActiveType)
{
case SetActiveType.activeSelf:
journalGameObject.SetActive(!journalGameObject.activeSelf);
firstPersonController.GetComponent<FirstPersonController>().enabled = !firstPersonController.GetComponent<FirstPersonController>().enabled;
if (!journalGameObject.activeSelf)
{
SaveJournal();
}
break;
case SetActiveType.activeInHierarchy:
journalGameObject.SetActive(!journalGameObject.activeInHierarchy);
firstPersonController.GetComponent<FirstPersonController>().enabled = !firstPersonController.GetComponent<FirstPersonController>().enabled;
if (!journalGameObject.activeInHierarchy)
{
SaveJournal();
}
break;
}
}
/// <summary>
/// Use for saving journal
/// File name is "journalID.journal"
/// </summary>
private void SaveJournal()
{
try
{
using (FileStream fs = new FileStream(Application.persistentDataPath + "/" + journalID + ".journal", FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(inputField.text);
sw.Flush();
}
}
}
catch(Exception e)
{
Debug.Log("Can't save journal! \nError: " + e.Message);
}
}
/// <summary>
/// Use for loading journal if one is available
/// </summary>
private void LoadJournal()
{
if(!File.Exists(Application.persistentDataPath + "/" + journalID + ".journal"))
{
Debug.Log("Journal with the name " + journalID + ".journal" + " can't be found, Load cancelled");
return;
}
try
{
using (FileStream fs = new FileStream(Application.persistentDataPath + "/" + journalID + ".journal", FileMode.Open))
{
using (StreamReader sr = new StreamReader(fs))
{
inputField.text = sr.ReadToEnd();
}
}
}
catch (Exception e)
{
Debug.Log("Can't load journal! \nError: " + e.Message);
}
}
}