- Home /
Convert Array into One String (Js)
Hello coders and devs, How would I go about getting an array (let's call it 'items') and putting it into one string (let's call it 'allitems')? I've currently got this script:
for (var i = 0; i < items.length; i++){
item = items[i];
allitems = ("" + item.transform.position.x).toString();
print (allitems);
}
I'm currently getting the Missing Method Exception: System.String.toString Error I want to put the transform.position.x of each item onto a string so it outputs to something like this:
32 55 60 43 405
or another example:
43 9 321 64 34 7 10 29 1 945 58 77
(They are exact integers and sorting doesn't matter)
Thanks, I appreciate your feedback and answers!
Answer by HarshadK · Nov 07, 2014 at 07:16 AM
This will do the trick. It's considered that items array contains your game objects of which you want to grab the transform:
var allItems : string = "";
for (var i : int = 0; i < items.length; i++){
allitems += " " + items[i].transform.position.x;
}
allItems.ToString();
Debug.Log(allItems);
Also its ToString() and not toString().
Thanks, however since it's in an update loop, it will constantly keep adding to itself which I don't want happening. I require it however to update realtime.
It won't keep adding to itself when the allItems variable is declared and initialized in the update before the for loop, which it is in this example.
You could also do:
for (i in items){
allitems += " " + i.transform.position.x;
}
It's basically the same thing, but cleaner in JS.