Keep getting this error on different Text boxes. Any help would be appreciated!
NullReferenceException: Object reference not set to an instance of an object textHolder+c__Iterator1.MoveNext () (at Assets/Script/textHolder.cs:27) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) textHolder:Start() (at Assets/Script/textHolder.cs:19)
The code:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class textHolder : MonoBehaviour {
public string talking;
private TextManager TMan;
public float speed = 0.1f;
//public string fullText;
//private string currentText = "";
public string[] talkingLines;
// Use this for initialization
void Start () {
TMan = FindObjectOfType <TextManager> ();
StartCoroutine(ShowText());
}
IEnumerator ShowText(){
for (int i = 0; i < talkingLines.Length; i++) {
talking = talkingLines[i];
this.GetComponent<Text> ().text = talking;
yield return new WaitForSeconds (1);
}
}
// Update is called once per framea
void Update () {
}
void OnTriggerStay2D (Collider2D other)
{
if (other.gameObject.name == "Player")
{
if (Input.GetKeyUp (KeyCode.Space))
{
//TMan.ShowBox (talking);
if (!TMan.talkingActive)
{
TMan.talkingLines = talkingLines;
TMan.currentLine = 0;
TMan.showTalking ();
}
}
}
}
}
I don't know which specific line of code the error is pointing to, but as you said, you're getting error on Text box. It could be the line this.GetComponent<Text> ().text = talking;
and if it is, then the reason is you don't have Text component attached to your gameobject. Please double check that.
Answer by Brylos · Nov 10, 2016 at 07:21 AM
I believe it has something to do with scripts detecting certain components. Check that you dont have any deleted scripts on objects and or dont have a public game object or component that isnt assigned to a script. I'm not completely sure that is how to fix it, but I hope this helps.