Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by unity_oVm0bd-LCrV4qQ · Feb 12, 2019 at 02:52 PM · textlisttimeexport problemreaction

How to export in a text file float numbers generated in a list

Hello everyone,

I'm totally new to coding. I am trying to develop a psychological test based on Reaction Times. In a script I created a list that during the session gets filled with 7 float numbers. The list is...

public List reactionTimes = new List();

After the session I would like to export the 7 reaction times in a text file on my Mac. So I created another Script, attached to an empty object in Unity, named SaveData. I was trying to experiment on this following some online tutorials but I can't figure out how it is intended to be done. Even though there is no error reported on the Scripts, nothing seems to happen. This is the code, adding using System.IO.

public class SaveData : MonoBehaviour {

 ReactionTime subjectresults; 

 // Use this for initialization
 void Start () {

     subjectresults = GetComponent <ReactionTime>();

 }

 // Update is called once per frame
 void Update () {

 }

 private void SaveToFile()
 {
     StreamWriter writer = new StreamWriter (@"path");

     foreach (float TR in subjectresults.reactionTimes) 
     {
         float output = TR;
         writer.WriteLine (output);
         writer.Close ();
     }
 }

}

What can I do to fix this? Thank you very much

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Hellium · Feb 12, 2019 at 04:11 PM

You are closing the writer before writing all the lines....

Use the following method instead:

  private void SaveToFile()
  {
     // Convert the floats to strings
     string[] lines = new string[subjectresults.reactionTimes.Length];
     for( int i = 0 ; i < subjectresults.reactionTimes.Length ; ++i )
         lines[i] = subjectresults.reactionTimes[i].ToString();

      // Write the lines into the file
     File.WriteAllLines(Application.persistentDataPath + "/reactionTimes.txt", lines);
  }


Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_oVm0bd-LCrV4qQ · Feb 13, 2019 at 11:13 AM 0
Share

Hello @Hellium thank you for answering,

I tried using your method but I still can't find a way to save this .txt on my desktop (for example). Actually I can't find the .txt anywhere on my $$anonymous$$ac (unfortunately I don't have a PC at the moment). Do you have any idea about what I'm missing?

Thanks

avatar image Hellium unity_oVm0bd-LCrV4qQ · Feb 13, 2019 at 11:25 AM 0
Share

On $$anonymous$$ac, Application.persistentDataPath points to /var/mobile/Containers/Data/Application/<guid>/Documents

If you want to save the file on the Desktop, you will have to change the path.

I am really not sure of the following, but you can give it a try:

 string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
 File.WriteAllLines(path + "/reactionTimes.txt", lines);
avatar image unity_oVm0bd-LCrV4qQ · Feb 15, 2019 at 01:51 PM 0
Share

Hello again @Hellium , I still can't figure out why this file I'm trying to save doesn't exist apparently anywhere on my mac. This is my actual code... The only differences from yours are: reactionTimes.Count ins$$anonymous$$d of "Lenght", because reactionTimes is a List, and I've added System.Environment, because with only Environment it was returning an error.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using System.IO;

public class SaveData : $$anonymous$$onoBehaviour {

 ReactionTime subjectresults; 


 // Use this for initialization
 void Start () {

     subjectresults = GetComponent <ReactionTime>();

 }

 // Update is called once per frame
 void Update () {

 }

 private void SaveToFile()
 {
     // Convert the floats to strings
     string[] lines = new string[subjectresults.reactionTimes.Count];
     for (int i = 0; i < subjectresults.reactionTimes.Count; ++i)
         lines [i] = subjectresults.reactionTimes [i].ToString ();
     // Write the lines into the file
     string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
     File.WriteAllLines (path + "/reactionTimes.rtf", lines);

 }

}

avatar image Hellium unity_oVm0bd-LCrV4qQ · Feb 15, 2019 at 02:02 PM 0
Share

How and when do ou call SaveToFile? Try to print the path in the SaveToFile function.

avatar image unity_oVm0bd-LCrV4qQ · Feb 27, 2019 at 10:13 AM 0
Share

Hi @Hellium ,

Thank you for your help. I realized I wasn't calling for SaveToFile. So I called it in the update function and it worked!

Have a good day!

avatar image Hellium unity_oVm0bd-LCrV4qQ · Feb 27, 2019 at 10:21 AM 0
Share

Don't call it too often in the Update function, only when you need it! Input/output operations are heavy.

If the answer solved your problem, don't forget to click on the Accept button below it

avatar image unity_oVm0bd-LCrV4qQ · Feb 27, 2019 at 10:27 AM 0
Share

Could it be reasonable to call it in the Updated function only when the list reaches 7 elements, which are the 7 reaction times that I need?

avatar image Hellium unity_oVm0bd-LCrV4qQ · Feb 27, 2019 at 10:40 AM 0
Share

I suggest you using a "flag" to know when the file has been saved:

  bool fileSaved = false;
  void Update ()
 {
     if( subjectresults.reactionTimes.Count == 7 && !fileSaved )
     {
         SaveToFile();
         fileSaved = true;
     }
  }

avatar image unity_oVm0bd-LCrV4qQ Hellium · Feb 27, 2019 at 12:15 PM 0
Share

Ok, but the condition !&fileSaved might not work with my last intent: I don't want to take too much of your time, but is it possible to start from the script as it is now to save - in the same text file - other Reaction Times calculated in other Unity Scenes, also specifying from which one I'm calculating them? I know I don't have enough competences to do this, but if this script is a good starting point I'll try to check some tutorials to finish this project!

Thank you

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

173 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Type out text in sync with audioclip 0 Answers

make list of questions from txt file and make it work in android. c# 1 Answer

How to get tooltips to pop up on certain levels, but not all of them 2 Answers

how to convert youtube videos duration time to seconds 0 Answers

What's wrong with this string list code? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges