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 LeeGibson · Jul 12, 2011 at 12:42 PM · javascriptvariablesxmlwhile-loopdeclaration

javascript: declaring a variable in 'while' loop

i have a script called xmlReader, which does exactly what it says: read strings from an xml-file. now i want to declare a variable for every string, but i'm stuck at how to do that exactly. the code is the following:

 import System.Xml;
 import System.IO;
 var xmlString : String;
 var i : int = 0;
 
 function Start()
 {
 var asset:TextAsset = Resources.Load("test 1");
   
     if(asset != null)
     {
     var reader : XmlTextReader = new XmlTextReader(new StringReader(asset.text));
 
     while(reader.Read())
     {
         if(reader.Name == "item")
         {
         Debug.Log(reader.Name + " label = " + reader.GetAttribute("label"));
         xmlString= reader.GetAttribute("label");
         Debug.Log(xmlString);
         i++;
         }
     }
     }
 }

i declared xmlString as global var in the beginning for testing so i could see if anything gets written inside the loop. i was hoping that i could use 'i' as an addition to every variable name, but i couldn't find any syntax that worked with that. i tried things like

 xmlString +i = reader.GetAttribute("label");

but it's only producing an error. so how can i dynamically declare variables in a loop?

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
Best Answer

Answer by Waz · Jul 12, 2011 at 12:56 PM

You can't. I think what you're looking for is an array:

 xmlString[i] = ...

Be sure your array is large enough:

 val xmlString = new String[100];
Comment
Add comment · Show 1 · 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 LeeGibson · Jul 12, 2011 at 01:09 PM 0
Share

that actually makes sense... i was so sure that it had to work somehow that i didn't even think about an array >.< thanks a lot!

avatar image
2

Answer by aldonaletto · Jul 12, 2011 at 01:09 PM

A simple way would be to declare xmlString as an Array:

   var xmlString = new Array();

And use it as any array:

     xmlString[i] = reader.GetAttribute("label");

The Array class is not the more efficient resource in the world, and should be avoided in Updates and other frequently called functions because it is a variant type: the system has a lot of behind-the-scene work to do (check content type, manage array size, etc.). But in your application it seems to be the best alternative, since you don't know beforehand how many lines will be read.

Comment
Add comment · Show 11 · 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 Eric5h5 · Jul 12, 2011 at 04:44 PM 0
Share

Array is almost never the best alternative, and it's not in this case either. The best would be List, which is much faster than Array, can be resized, and doesn't have dynamic typing issues.

avatar image Dreamblur · Jul 12, 2011 at 09:06 PM 1
Share

@aldonaletto List is in the .Net library as well. http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

avatar image Joshua · Jul 12, 2011 at 09:12 PM 2
Share

It's simple dotNet, so yes, it's available to javascript users.

You need to of course import the namespace.

 import System.Collections.Generic;

 var myTransformList : List.< Transform > = new List.< Transform >;
 myTransformList.Add( transform );
 myTransformList[ i ] = transform; 


http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F#Generic_List

Read both pages through carefully, and you'll save yourself a lot of array-unhappiness :)

avatar image Eric5h5 · Jul 12, 2011 at 09:17 PM 2
Share

The only difference between Lists in C# and JS is that you do (for example) List<int> in C# and List.<int> in JS.

avatar image Eric5h5 · Jul 13, 2011 at 08:10 AM 1
Share

Specifying a location doesn't add it; the element must already exist. testList[1] refers to the second element of the list, since arrays always start at 0.

Show more comments

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

7 People are following this question.

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

Related Questions

How i can a variable clasification in javascript? 3 Answers

Best way to handle a lot of variables 3 Answers

GameObject.Find either can't be called or is in the wrong scope. 0 Answers

Changing a Variable in a script from another Object 1 Answer

Some Basic Conversions from JavaScript to C# 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