• 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 /
avatar image
0
Question by schetty · Mar 28, 2013 at 04:59 AM · filefile-io

Problem on writing the copied array of string on text pad

Hi Every One,

I have come up with the new issue, here i want to write the my string data to my note pad through the scripting. When i write it my last array string only displayed because of over writing.

Actually i want to copy the string from one array to other string array and then i want to write it on note pad. Here i gave my script, if you have any solution please tell me.

 using UnityEngine;
 using System.Collections;
 using System.IO;
 using System.Text;
 
 public class Black : MonoBehaviour { 
 
     public static bool temp;    
     public string[] test;
     public string[] gf;
 
     // Use this for initialization
     void Start () {       
 
         temp = false;   
     }   
 
     // Update is called once per frame
     void Update () {       
 
         if(temp)
         {      
 
             for(int i =0; i < test.Length; i++)
             {
                 gf = new string[test.Length];              
 
                 gf[i] = test[i];
                 File.WriteAllLines(Application.dataPath+"/Test.txt", gf);
 
             }        }
     }   
 
     void OnMouseUpAsButton()
     {
         temp = !temp;      
 
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by iwaldrop · Mar 28, 2013 at 05:07 AM

You're declaring the array and writing the file on each iteration of the loop! Do the following instead:

 void Update ()
 {        
         if (temp)
         {
             gf = new string[test.Length];  
             for(int i =0; i < test.Length; i++)
                 gf[i] = test[i];
             File.WriteAllLines(Application.dataPath+"/Test.txt", gf);
         }
 }   
Comment
Add comment · Show 3 · 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 iwaldrop · Mar 28, 2013 at 05:11 AM 0
Share

But, of course, you could just do:

 gf = test;

Or just write the contents of test to a file. It's best not to iterate though an array unless you're doing something to the data. :)

avatar image iwaldrop · Mar 28, 2013 at 05:31 AM 0
Share

Also, ins$$anonymous$$d of checking if temp is true in the update loop, just have your On$$anonymous$$ouseUp function run another function that updates your list.

avatar image schetty · Mar 28, 2013 at 08:13 AM 0
Share

but when i use this method the string are over writing . Assume 5 arrays here i am using this method to copy the one to other, when i write to my textpad onlt last string print others were blank.

avatar image
0

Answer by sparkzbarca · Mar 28, 2013 at 05:08 AM

pretty sure write all lines doesnt append (that is add to a file) but instead overwrites.

You'd want to do something like

 string MyString;
 
 foreach (string word in MyString){
 MyString += word;
 }

That will be everything you might want to insert spaces between. like

MyString += " " + world;

you can thing just write the string to the file now that you've got it all in a string.

you can copy from one string array to another easily enough

 string[] OldArray;
 string[] NewArray;
 
 OldArray = new string[size];
 // fill old array
 
 system.array.copy(OldArray,NewArray,OldArray.length);


code has not been error checked its just pseudo code. mark as answered please :)

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Is there a FileChooser available for GUI? 1 Answer

Loading an asset into the game post-build 0 Answers

Reading/Writing to a text file on non-windows platforms 1 Answer

File Read-Write Error in iOS 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