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 kumarsmurthy · Jan 10, 2014 at 04:22 AM · javascriptarraysmultidimensional array

Why can't I push and pop from string array?

I have an array that I will use to store Strings and save that using PlayerPrefsX(ArrayPrefs2). I need to add and replace elements of this array a lot of times.

private var Array1 : String[]; private var Array2 = new Array();

If I use Array1 I cannot use any functions like push/pop!(Only function I get is Length :/ ) If I use Array2 I need to convert that to a String array to store it in PlayerPrefsX.

n I won't even open my mouth about how my 2D arrays suck! I am using Unityscript and I know I am missing something VERY FUNDAMENTAL. :(

Comment
Add comment · Show 2
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 Santosh Patil · Jan 10, 2014 at 05:15 AM 0
Share

@kumarsmurthy, you can use List for push and pop elements.

using UnityEngine; using System.Collections; using System.Collections.Generic; public class IntroScript : $$anonymous$$onoBehaviour {

 List <string> iList;
 // Use this for initialization
 void Start () 
 {
     iList = new List<string>();
     for(int i = 0;i<10;i++)
     {
         iList.Add("User_"+i);
     }

     for(int j = 0;j< iList.Count;j++)
     {
         print ("User at "+j +" is  "+iList[j]);
     }

     iList.RemoveAt(5);
     print ("After removing element");
     for(int j1 = 0;j1< iList.Count;j1++)
     {
         print ("User at "+j1 +" is  "+iList[j1]);
     }
 }

}

avatar image Eric5h5 · Jan 10, 2014 at 06:10 AM 0
Share

The question is about Unityscript, not C#.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Eric5h5 · Jan 10, 2014 at 05:07 AM

Built-in arrays are fixed size and have no add/remove functions. Never use the Array class; it's obsolete (slow and untyped). Use a generic List instead. http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F ArrayPrefs2 only uses built-in arrays, but it's trivial to convert List to a built-in array and back.

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 kumarsmurthy · Jan 10, 2014 at 05:35 AM 0
Share

Thank you Eric and Santosh for your replies. I did go through "Which kind of Array should I use" before I posted that question, BUT I didn't read the complete article and used built in arrays!! I have another question, if I use a generic List, is it possible to make that 2D in future?? I have 3 lists of variable lengths. Even though I can use 3 lists, I think it will reduce a lot of code if I can access it something like myArray[0][X].

I saw that I can use a JaggedArray but I am too scared to try that out just yet.

avatar image Eric5h5 · Jan 10, 2014 at 06:10 AM 0
Share

You can have a List of Lists, yes.

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

19 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 avatar image avatar image

Related Questions

Javascript / Unityscript adding elements of an array 2 Answers

Multidimensional array variable size (JS) 1 Answer

Referencing data in pre-defined array 1 Answer

JavaScript 3 Arrays questions 1 Answer

How to Create Dynamic Multidimensional Array? 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