Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 HolBol · Aug 31, 2013 at 03:35 PM · arraysstringxmlint

Converting String Array to Int.

Hi there.

I'm having an issue with converting an array of strings into an array of integers. What I've done, is get an array of strings from XML and used Split() to convert that into an array, which gets me the numbers 2,2,2,1 as it should. Then, I'm using this code to loop through each member of the string array and convert it to an integer, to create an integer array.

The following code is in UnityScript.

 var movesRow : String; 
         
         var movesString : String[];
     var moves : int[];
             
    movesRow = EnemyXML.GetValue("enemies>0>enemy>" + i + ">choices>0>_text");
     //this gets me a value of "2,2,2,1" as a string.
         //this here then splits it up into a string array
     movesString = movesRow.Split(','[0]);
         //and then prints it, giving a length of 4, as expected    
     print (movesString.length);
        // here's where i loop through them. 
     for (var a = 0; a < movesString.length; a++){
 
                 //this prints each value, 2, 2, 2, 1 as expected.
         print(movesString[a]);
 
                 //so I assign each value of the string array to the int array, parsing each one
 
         moves[a] = int.Parse(movesString[a]);
                 
     }

But I get a nullreferenceexception, on the int.Parse line. I can't figure quite what's going wrong here. Any help would be great!

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

Answer by HolBol · Aug 31, 2013 at 04:33 PM

Answered- used

var movesRow : String;

 var movesString : String[];
 var moves : int[] = new int[4];
  
 movesRow = EnemyXML.GetValue("enemies>0>enemy>" + i + ">choices>0>_text");
 //this gets me a value of "2,2,2,1" as a string.
 //this here then splits it up into a string array
 movesString = movesRow.Split(','[0]);
 //and then prints it, giving a length of 4, as expected
 print (movesString.length);
 // here's where i loop through them.
 for (var a = 0; a < movesString.length; a++){
  
 //this prints each value, 2, 2, 2, 1 as expected.
 print(movesString[a]);
  
 //so I assign each value of the string array to the int array, parsing each one
  
 moves[a] = int.Parse(movesString[a]);
  
 }

var moves : int[] = new int[4]; this here worked, because there can only ever be 4 values that come from the XML. So yes, TrickyHandz was right, the length did need to be set first.

Comment
Add comment · 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
1

Answer by TrickyHandz · Aug 31, 2013 at 03:59 PM

The nullreferenceexception is due to the way arrays function. You can't assign a value to an entry in the array until you establish the length of the array. Below is a small change to resize the array prior to assigning values:

 var movesRow : String;
 var movesString : String[];
 var moves : int[];
 
 movesRow = EnemyXML.GetValue("enemies>0>enemy>" + i + ">choices>0>_text");
 //this gets me a value of "2,2,2,1" as a string.
 //this here then splits it up into a string array
 movesString = movesRow.Split(','[0]);
 
 // assign a new array
 moves = int[movesRow.length];
 
 //and then prints it, giving a length of 4, as expected 
 print (movesString.length);
 // here's where i loop through them. 
 for (var a = 0; a < movesString.length; a++)
 {
     //this prints each value, 2, 2, 2, 1 as expected.
     print(movesString[a]);
 
     moves[a] = int.Parse(movesString[a]);
     
 }

EDIT: Code edited due to access error

Comment
Add comment · Show 2 · 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 HolBol · Aug 31, 2013 at 04:25 PM 0
Share

Hasn't worked- moves.length = movesString.length give me an error of Property 'UnityScript.Lang.Extensions.length' is read only. I know that cant be right- because I'm sure you can set the length manually. Any idea?

EDIT : this won't work- built in arrays are of a fixed size. So you can't modify length directly.

avatar image TrickyHandz · Aug 31, 2013 at 04:39 PM 0
Share

Sorry about that...I forgot you can't resize a built in arrays. Admittedly, I have been using generics for most of my stuff. I edited to the code to make a new array with an appropriate length rather than trying to resize. Tell me if that works for you.

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

17 People are following this question.

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

Related Questions

public string array controlled by an int in the inspector 1 Answer

Changing a GUI String to read as a INT 2 Answers

C# Problem with assigning values arrays 1 Answer

pick a random int with the value of 1 from an array 2 Answers

Convert Array to String 3 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