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 /
avatar image
0
Question by blitzen · Sep 24, 2011 at 03:26 AM · arraystringdynamicpush

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
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
1

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());
Comment
Add comment · Show 4 · 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 blitzen · Sep 24, 2011 at 05:40 AM 0
Share

By "list" do you mean "new Array()"? Code please.

avatar image blitzen · Sep 25, 2011 at 06:15 AM 0
Share

It looks like a hybrid of C# and JavaScript. In the latter, I got an unknown identifier error for '

avatar image Bunny83 · Sep 25, 2011 at 10:37 AM 2
Share

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.

avatar image karl_ · Sep 25, 2011 at 03:57 PM 0
Share

I've updated the post to fix the ToArray() syntax. Thanks for pointing that out.

avatar image
1

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.

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 msknapp · Sep 24, 2011 at 02:26 PM 0
Share

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.

avatar image karl_ · Sep 24, 2011 at 04:50 PM 1
Share

You can use a list in JS. Just remember to import System.Collections.Generic

avatar image blitzen · Sep 25, 2011 at 03:15 PM 0
Share

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

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

6 People are following this question.

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

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


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