- Home /
Concatenating to a String[] Array Dynamically
The task is to dynamically create a String[] of arbitrary length and add strings to it. It needs to be passed to the 3rd argument of GUI.Toolbar which takes a String[]. I have tried the following declarations:
var toolbarStrings:String[]=[""];
var toolbarStrings:String[]=[];
var toolbarStrings:String[];
var toolbarStrings:String=[];
var toolbarStrings=[""];
var toolbarStrings=new Array();
var toolbarStrings=new Array(String);
To each of these I have attempted the following initialization in Awake():
for(var i:int=0;i]]
(On a side note, not being able to include square brackets in a Google search, or find information through Google on how to do so, is another matter.)
Comment
Answer by karl_ · Sep 24, 2011 at 04:04 AM
Why not use a list? Then, in your GUI.Toolbar use the ToArray() function.
import System.Collections.Generic;
var toolbarStrings : List<String> = new List.<String>();
then in your initialization
for(var i:int=0;i<=5;i++)
{
toolbarStrings.Add("Number "+i);
}
and in your GUI:
toolbarInt = GUI.Toolbar (Rect (25, 25, 250, 30), toolbarInt, toolbarStrings.ToArray());
It looks like a hybrid of C# and JavaScript. In the latter, I got an unknown identifier error for '
Yes, the UnityScript syntax is a bit different to the C# syntax: The "generic brackets" needs to be seperated by a dot:
var toolbarStrings : List.<String> = new List.<String>();
or even that way thanks to type inference:
var toolbarStrings = new List.<String>();
btw. the conversion into a native array should be just:
toolbarStrings.ToArray()
I'm also a C# user but UnityScript has the same power since both languages compile to $$anonymous$$ono / .NET
I prefer C# because i just don't like the JavaScript-like syntax.
I've updated the post to fix the ToArray() syntax. Thanks for pointing that out.
Answer by rejj · Sep 24, 2011 at 11:25 AM
You could be running in to issues with the differences between javascript native array types, and Unity script's array types.
I have successfully got the following to run:
var toolbarStrings:Array = new Array();
var toolbarInt:int = 0;
function Awake () {
for (var i:int = 0; i <= 5; ++i) {
toolbarStrings.Push('# ' + i);
}
}
function OnGUI() {
toolbarInt = GUI.Toolbar (Rect (25, 25, 250, 30), toolbarInt, toolbarStrings.ToBuiltin(String));
}
The key part being the call to .ToBuiltin(String)
to convert the Unity array to a Javascript array of strings.
Personally, I would use C# rather than Javascript, and then you would be able to use the .net List<> type to add strings dynamically, and then call .ToArray()
to return you an array of strings.
I agree with these other posts. By "List" they mean the .net class System.Collections.List. or System.Collections.generics.List.
var s:String[]; // will not automatically resize!
var t:Array; // will automatically resize with the push function!
// so this results in index out of bounds exception: s[0] = "any string"; // because s is of size 0, you cannot define the first element.
You can use a list in JS. Just remember to import System.Collections.Generic
Worked, thanks. The bridge needed between what could be dynamically push()'ed to (i.e. Array()), and what could be passed to GUI.Toolbar (i.e. String[]), was indeed "Array.ToBuiltin(String)". Funny-looking, but functional.
Your answer
Follow this Question
Related Questions
C# Incrementing a String Array 1 Answer
C# String Array Has Missing or Incorrect Keycode Strings 2 Answers
Adding elements to a 2d array. 2 Answers
Extending the Array class? 1 Answer
Array - Convert Object into Int 5 Answers