Button not working when using NuitrackScripts

Hi guys! When I add “NuitrackScripts” in Folder “Prefabs”, my button is not working, although it was working properly before adding “NuitrackScripts”
Code:
private void OnGUI()
{
if (GUI.Button(new Rect(0, 10, 80, 20), “Click”))
{
save_excel();
}
}
public void save_excel()
{
if (File.Exists(filename))
{
File.Delete(filename);
}
TextWriter tw = new StreamWriter(filename, false);
tw.WriteLine(“Left Knee, Left Hip, Time”);
tw.Close();
tw = new StreamWriter(filename, true);
foreach (var knee in angle_Knee)
{
foreach (var hip in angle_Hip)
{
foreach (var time in data_time)
{
tw.Write(knee + “,” + hip + “,” + time);
tw.Close();
}
}
}
Debug.Log(“Ok!”);
}

video-1626535452

Hi!

This problem is not related to Nuitrack, the problem is in your code.
I changed your example a little so that it works correctly, here’s a look:

private void OnGUI()
{
    if (GUI.Button(new Rect(0, 10, 80, 20), "Click"))
    {
        save_excel();
    }
}
public void save_excel()
{
    if (File.Exists(filename))
    {
        File.Delete(filename);
    }

    using (TextWriter tw = new StreamWriter(filename, false))
    {
        tw.WriteLine("Left Knee, Left Hip, Time");

        foreach (var knee in angle_Knee)
        {
            foreach (var hip in angle_Hip)
            {
                foreach (var time in data_time)
                {
                    tw.WriteLine(knee + "," + hip + "," + time);
                }
            }
        }
    }
    Debug.Log("Ok!");
}

I used “using”, as it is more correct and stable than closing the file write stream manually.

You closed the write stream in a nested loop (tw.Close()). because of this, writing to the file failed on the next iteration.

I assume that this error did not occur without Nuitrack, due to the fact that there was no data to write, and none of the loops were executed.

2 Likes

Thanks for your answer. But I try to add some data and this problem is still the same. I think it lies elsewhere!