- Home /
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;
}
}
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);
}
}
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. :)
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.
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.
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 :)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Material doesn't have a color property '_Color' 4 Answers
File Browser that works on ipad + samsung + desktop 0 Answers