- Home /
unable to get InputFiled.text after using multiple GameObject.Find in second script.,Why does InputField.text doesn't returns written text sometimes?
The InputField.text
of New_Quest()
method of Dialogue_Handler.cs
starts returning blank after adding extra GameObject.Find()
methods in a second script. Also it doesn't works if i disable the things in chronological order.
Dialogue_menu_handler.cs file:
using System.Collections.Generic;
using UnityEngine;
namespace DialogueSystem
{
public class Dialogue_Menu_handler : MonoBehaviour
{
public List<GameObject> gobs = new List<GameObject>();
void Awake()
{
gobs.Add(GameObject.Find("Quest Maker"));
gobs.Add(GameObject.Find("Dialogue Maker"));
gobs.Add(GameObject.Find("Option Maker"));
gobs.Add(GameObject.Find("Dialogue Joiner"));
}
void Start()
{
Switch_to_quest_menu();
}
public void Switch_to_quest_menu()
{
for (int j = 0; j < gobs.Count; j++)
{
if (j != 0)
{
gobs[j].SetActive(false);
}
else
{
gobs[j].SetActive(true);
}
}
}
Above script is used to switch between UI panels. upon using the switch to quest menu()
method in Unity's Awake()
method.
Dialogue_handler.cs File:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace DialogueSystem
{
public class Dialogue_Handler : MonoBehaviour
{
public List<Quest> All_Quests = new List<Quest>();
/*
public string Quest = "nothing";
public InputField DialogID;
public InputField Actor;
public InputField DialogueString;
public Toggle hasOptions;
public InputField no_op;
public Toggle hasCode;
public InputField Code;
public Dialogue nextNode = null;
*/
public InputField QuestOrder;
public InputField QuestName;/*
public Text QuestLabel;
public string test = "null";
*/
public Text QuestResult;
public void Start()
{
/*
this.DialogID = GameObject.Find("Dialogue ID").GetComponent<InputField>() ;
this.Actor = GameObject.Find("Actor ID").GetComponent<InputField>() ;
this.DialogueString = GameObject.Find("Dialogue String").GetComponent<InputField>() ;
this.hasOptions = GameObject.Find("Option Toggle").GetComponent<Toggle>() ;
this.no_op = GameObject.Find("How many?").GetComponent<InputField>() ;
this.hasCode = GameObject.Find("Code Toggle").GetComponent<Toggle>() ;
this.Code = GameObject.Find("What's the Code").GetComponent<InputField>() ;
*/
this.QuestOrder = GameObject.Find("Quest Order").GetComponent<InputField>() ;
this.QuestName = GameObject.Find("Quest Field").GetComponent<InputField>() ;
// QuestLabel = GameObject.Find("Quest Name").GetComponent<Text>();
QuestResult = GameObject.Find("Quest Result").GetComponent<Text>() as Text;
// this.nextNode = null;
//Dialogue placeholder = new Dialogue("Quest Name",DialogID, Actor, DialogueString, hasOptions, no_op, hasCode, Code,null);
}
public void NewQuest() // this is called on button click
{
Debug.Log(this.QuestName.text);
//All_Quests.Add(new Quest(int.Parse(QuestOrder.text), QuestName.text));
// QuestLabel.text = QuestName.text;
}
Also the Debug.Log(QuestName.text)
returns blank again if i initialise the commented data in Dialoge_Handler.cs
I have tried to find all gameobjects using GameObject.Find()
in unity's in-built Awake
method and then disabling the unnecessary panels in UI in the start method so all the Gameobject variables don't give a NullrefrenceException
. But upon doing this the inputfield's are not returning any text put into them on runtime.
It only works if all the conditions follow:
If i don't initialize the commented Fields in
Dialogue_Handler
and not find them in awake or start method.Using the method
switch_to_Quest_menu()
inAwake
method rather than start method. i am stumped at this as my initial thought was that maybeGameobject.find
is not able to find the gameobjects before i disable them, as a result of which i tried to find all the gameobjects inAwake()
and disabled them inStart()
but this also didn't work.
Question:
How could i get the InputField.text
to give me runtime value after i initialize and find and store the gameobjects in alist or array?
Your answer
![](https://koobas.hobune.stream/wayback/20220613022338im_/https://answers.unity.com/themes/thub/images/avi.jpg)