UI element stops script from reading a txt file

Hello community,
the error seems to sound pretty weird, so let me just explain it:
I am working on an augmented reality app (with vuforia). Therefore I added some UI buttons to show and hide different objects when a picture is detected. The objects to show and hide are fore example three 3D text objects. I take a txt file to change the content of the 3D texts. The script I use therefore is this one:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

public class OfflineData : MonoBehaviour {

    // Input Parameter
    [Header("Namen", order=1)]
    public GameObject[] Namen;

    [Header("Nachrichten", order=2)]
    public GameObject[] Nachrichten;

    //[Header("Fotos", order=3)]
    //public GameObject[] Fotos;

    // Private Members
    private List<DataObject> DataObjects;
    private int CurrentIndex = 0;

    // Use this for initialization
    void Start () {

        // Dateiinhalt laden
        var sr = new StreamReader(Application.dataPath + "/PastNotes.txt");
        var fileContents = sr.ReadToEnd();
        sr.Close();

        // Dateiinhalt parsen
        this.DataObjects = fileContents
            .Split('\n')
            .Select(x => x.Split(';'))
            .Select(line => new DataObject { Person = line[0], Message = line[1]}) //PictureUrl = line[2]
            .ToList();

        StartCoroutine (ChangeNews ());
    }

    // Update is called once per frame
    IEnumerator ChangeNews () {

        /*
        // Texte setzen
        (this.Nachrichten[0].GetComponent(typeof(TextMesh)) as TextMesh).text = this.DataObjects.ElementAt(this.CurrentIndex + 0).Message;
        (this.Nachrichten[1].GetComponent(typeof(TextMesh)) as TextMesh).text = this.DataObjects.ElementAt(this.CurrentIndex + 1).Message;
        (this.Nachrichten[2].GetComponent(typeof(TextMesh)) as TextMesh).text = this.DataObjects.ElementAt(this.CurrentIndex + 2).Message;
        */

        var firstMessage = this.Nachrichten.ElementAtOrDefault (0).GetComponent (typeof(LineBreaker)) as LineBreaker;
        firstMessage.UnwrappedText = this.DataObjects.ElementAt(this.CurrentIndex + 0).Message;
        firstMessage.NeedsLayout = true;
        firstMessage.MaxWidth = 120;

        var secondMessage = this.Nachrichten.ElementAtOrDefault (1).GetComponent (typeof(LineBreaker)) as LineBreaker;
        secondMessage.UnwrappedText = this.DataObjects.ElementAt(this.CurrentIndex + 1).Message;
        secondMessage.NeedsLayout = true;
        secondMessage.MaxWidth = 120;

        var thirdMessage = this.Nachrichten.ElementAtOrDefault (2).GetComponent (typeof(LineBreaker)) as LineBreaker;
        thirdMessage.UnwrappedText = this.DataObjects.ElementAt(this.CurrentIndex + 2).Message;
        thirdMessage.NeedsLayout = true;
        thirdMessage.MaxWidth = 120;


        // Name setzen
        (this.Namen[0].GetComponent(typeof(TextMesh)) as TextMesh).text = this.DataObjects.ElementAt(this.CurrentIndex + 0).Person;
        (this.Namen[1].GetComponent(typeof(TextMesh)) as TextMesh).text = this.DataObjects.ElementAt(this.CurrentIndex + 1).Person;
        (this.Namen[2].GetComponent(typeof(TextMesh)) as TextMesh).text = this.DataObjects.ElementAt(this.CurrentIndex + 2).Person;


        // Warten
        yield return new WaitForSeconds (0.5f);

        // Aktuelle Zählervariable hochzählen
        if (this.CurrentIndex + 3 == this.DataObjects.Count())
            this.CurrentIndex = 0;
        else
            this.CurrentIndex++;

        // Loop wiederholen
        StartCoroutine(ChangeNews());       
    }

    class DataObject {
        public string Person;
        public string Message;
        //public string PictureUrl;
    }
}

The “LineBreaker” script I’m refering to is this one:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(TextMesh))]
public class LineBreaker : MonoBehaviour
{
    TextMesh TheMesh;
    public string UnwrappedText;
    public float MaxWidth;
    public bool NeedsLayout = true;
    public bool ConvertNewLines = false;

    void Start()
    {
        TheMesh = GetComponent<TextMesh>();
        if (ConvertNewLines)
            UnwrappedText = "» " + UnwrappedText.Replace("\\n", System.Environment.NewLine) + " «";
    }

    string BreakPartIfNeeded(string part)
    {
        string saveText = TheMesh.text;
        TheMesh.text = part;

        if (TheMesh.GetComponent<Renderer>().bounds.extents.x > MaxWidth)
        {
            string remaining = part;
            part = "";
            while (true)
            {
                int len;
                for (len = 2; len <= remaining.Length; len++)
                {
                    TheMesh.text = remaining.Substring(0, len);
                    if (TheMesh.GetComponent<Renderer>().bounds.extents.x > MaxWidth)
                    {
                        len--;
                        break;
                    }
                }
                if (len >= remaining.Length)
                {
                    part += remaining;
                    break;
                }
                part += remaining.Substring(0, len) + System.Environment.NewLine;
                remaining = remaining.Substring(len);
            }

            part = part.TrimEnd();
        }

        TheMesh.text = saveText;

        return part;
    }

    void Update()
    {
        if (!NeedsLayout)
            return;
        NeedsLayout = false;
        if (MaxWidth == 0)
        {
            TheMesh.text = UnwrappedText;
            return;
        }
        string builder = "";
        string text = UnwrappedText;
        TheMesh.text = "";
        string[] parts = text.Split(' ');
        for (int i = 0; i < parts.Length; i++)
        {
            string part = BreakPartIfNeeded(parts[i]);
            TheMesh.text += part + " ";
            if (TheMesh.GetComponent<Renderer>().bounds.extents.x > MaxWidth)
            {
                TheMesh.text = builder.TrimEnd() + System.Environment.NewLine + part + " ";
            }
            builder = TheMesh.text;
        }
    }
}

and the script to show and hide objects is this one:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ARObjectMod : MonoBehaviour {
    public GameObject[] WetObjs;
    public GameObject[] NewsObjs;
    public GameObject[] MultiObjs;
    public GameObject[] SMObjs;

    public Button wetterButton;
    public Button newsButton;
    public Button multiButton;
    public Button smButton;

    private Button w;
    private Button n;
    private Button mm;
    private Button sm;

    private ColorBlock wb;
    private ColorBlock nb;
    private ColorBlock mmb;
    private ColorBlock smb;

    private bool WetterBool = true;
    private bool NewsBool = false;
    private bool MultiBool = false;
    private bool SocialBool = false;


    // Use this for initialization
    void Start () {
        wetterButton.GetComponent<Button> ();
        newsButton.GetComponent<Button> ();
        multiButton.GetComponent<Button> ();
        smButton.GetComponent<Button> ();

        w = wetterButton.GetComponent<Button>();
        wb = w.colors;

        n = newsButton.GetComponent<Button>();
        nb = n.colors;

        mm = multiButton.GetComponent<Button> ();
        mmb = mm.colors;

        sm = smButton.GetComponent<Button> ();
        smb = sm.colors;


    }

    void Update() {

        if (WetterBool == true) {
            //NewsBool = false;
            //MultiBool = false;
            //SocialBool = false;

            wb.normalColor = new Color32(0, 255, 0, 255);
            wb.highlightedColor = new Color32(0, 255, 0, 255);
            w.colors = wb;


            for (int i = 0; i < this.WetObjs.Length; i++) {
                this.WetObjs [i].SetActive (true);
            }

            for (int x = 0; x < this.NewsObjs.Length; x++) {
                this.NewsObjs [x].SetActive (false);
            }

            for (int y = 0; y < this.MultiObjs.Length; y++) {
                this.MultiObjs [y].SetActive (false);
            }

            for (int z = 0; z < this.SMObjs.Length; z++) {
                this.SMObjs [z].SetActive (false);
            }

        } else {
            wb.normalColor = new Color32 (71, 71, 71, 255);
            wb.highlightedColor = new Color32 (71, 71, 71, 255);
            w.colors = wb;
        }

        if (NewsBool == true) {
            //WetterBool = false;
            //MultiBool = false;
            //SocialBool = false;

            nb.normalColor = new Color32(0, 255, 0, 255);
            nb.highlightedColor = new Color32(0, 255, 0, 255);
            n.colors = nb;


            for (int i = 0; i < this.WetObjs.Length; i++) {
                this.WetObjs [i].SetActive (false);
            }

            for (int x = 0; x < this.NewsObjs.Length; x++) {
                this.NewsObjs [x].SetActive (true);
            }

            for (int y = 0; y < this.MultiObjs.Length; y++) {
                this.MultiObjs [y].SetActive (false);
            }

            for (int z = 0; z < this.SMObjs.Length; z++) {
                this.SMObjs [z].SetActive (false);
            }

        } else {
            nb.normalColor = new Color32 (71, 71, 71, 255);
            nb.highlightedColor = new Color32 (71, 71, 71, 255);
            n.colors = nb;
        }

        if (MultiBool == true) {
            //WetterBool = false;
            //NewsBool = false;
            //SocialBool = false;

            mmb.normalColor = new Color32(0, 255, 0, 255);
            mmb.highlightedColor = new Color32(0, 255, 0, 255);
            mm.colors = mmb;

            for (int i = 0; i < this.WetObjs.Length; i++) {
                this.WetObjs [i].SetActive (false);
            }

            for (int x = 0; x < this.NewsObjs.Length; x++) {
                this.NewsObjs [x].SetActive (false);
            }

            for (int y = 0; y < this.MultiObjs.Length; y++) {
                this.MultiObjs [y].SetActive (true);
            }

            for (int z = 0; z < this.SMObjs.Length; z++) {
                this.SMObjs [z].SetActive (false);
            }

        } else {
            mmb.normalColor = new Color32 (71, 71, 71, 255);
            mmb.highlightedColor = new Color32 (71, 71, 71, 255);
            mm.colors = mmb;
        }

        if (SocialBool == true) {
            //WetterBool = false;
            //NewsBool = false;
            //MultiBool = false;

            smb.normalColor = new Color32(0, 255, 0, 255);
            smb.highlightedColor = new Color32(0, 255, 0, 255);
            sm.colors = smb;

            for (int i = 0; i < this.WetObjs.Length; i++) {
                this.WetObjs [i].SetActive (false);
            }

            for (int x = 0; x < this.NewsObjs.Length; x++) {
                this.NewsObjs [x].SetActive (false);
            }

            for (int y = 0; y < this.MultiObjs.Length; y++) {
                this.MultiObjs [y].SetActive (false);
            }

            for (int z = 0; z < this.SMObjs.Length; z++) {
                this.SMObjs [z].SetActive (true);
            }

        } else {
            smb.normalColor = new Color32 (71, 71, 71, 255);
            smb.highlightedColor = new Color32 (71, 71, 71, 255);
            sm.colors = smb;
        }


    }

    public void startWetter () {
        WetterBool = true;
        NewsBool = false;
        MultiBool = false;
        SocialBool = false;

    }

    public void startNews () {
        WetterBool = false;
        NewsBool = true;
        MultiBool = false;
        SocialBool = false;
    }

    public void startMulti() {
        WetterBool = false;
        NewsBool = false;
        MultiBool = true;
        SocialBool = false;
    }

    public void startSocial() {
        WetterBool = false;
        NewsBool = false;
        MultiBool = false;
        SocialBool = true;
    }
}

My problem is:
I’ve grouped the three 3D Text objects together and connected this with the ARObjectMod script to show and hide them. But as long as the objects are connected to the script, the Offline-Data script stops working. The texts are loading in, but they stop rotating.
Please help me! If you have any questions, just ask. I am totally clueless what causes the problem.

I didn’t follow all that — in particular, I don’t see what is supposed to be making the text objects rotate. But I suspect the problem is just that you’re disabling (calling SetActive(false)) your text objects, which of course disables all MonoBehaviours on them. That includes pausing any Coroutines that may be running.


The app looks like this. On the left I’ve got the green button activated. On the right are three 3D texts. There are about 8 text lines that are rotating (text line 2 should get on first position, line 3 on 2nd and on third position a new text line should be shown). But as long as I use the SetActive command, the text won’t be rotating. How can I get this to work correct?

Well of course if you deactivate them, they do not draw, do not run Coroutines, etc.

I still don’t quite understand what you’re trying to do here, but I bet if you pause the game in the non-working state, and study what objects are active or inactive in the hierarchy using the Inspector, it will quickly become obvious what’s going on.

I found the solution. I just had to replace the “void Start” function with “void OnEnable”. Now it works!