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
3
Question by Story Specialist · Sep 14, 2011 at 08:49 AM · javascriptiterategeneric list

Iterate through Generic List

Hey all,

I'm working on a little project to practice both my narrative design skills, as well as my Unity basics. Without explaining the entire thing, I can say that what I want is for the player to be able to collects phrases (Strings) to form a piece of text. Eventually, I want them to be able to remove or rearrange the phrases as well, but I'm taking it one step at a time.

After doing a bit of research, I figured using a Generic List was best. What I want to do is this: add items (Strings) to the List, then iterate through that List and spawn a GUI.Button for each item in the List (each button having its own space).

I came up with this (I work in Javascript):

 import System.Collections.Generic;
 
 var phraseList = new List.<String>();
 
 function OnGUI () {
 
         phraseList.ForEach (
             function() {
                 if (GUI.Button (Rect (100, 20 * phraseList.Item, 200, 20), "button")) {
                     Debug.Log ("Box #" + phraseList.Item + " is working");
                 }
             }
         );
         
 }
 
 function Update () {
     if (Input.GetKeyUp ("z")) {
         phraseList.Add("Test string 1");
         phraseList.Add("Test string 2");
         phraseList.Add("Test string 3");
         phraseList.ForEach (print);
         
     }
 }

But I get the following error message: "The property 'System.Collections.Generic.List.Item' cannot be used without parameters."

What kind of property do I need for "the current item", if you will? I hope you guys can help a beginner out!

Thanks in advance,

Veliremus

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

Answer by Story Specialist · Sep 15, 2011 at 02:38 PM

After receiving two answers by @syclamoth and @Erich5h5, I managed to get the generic List working in Javascript.

Javascript (solution provided by Eric5h5):


 import System.Collections.Generic;
 
 var phraseList = new List.<String>();
 
 function OnGUI () {
     
     var i : int = 0;
     for (str in phraseList) {
         if (GUI.Button (Rect (100, 20 * i, 200, 20), str)) {
             Debug.Log ("Box #" + i + " is working!");
         }
     i++;
     }
 
 }
     
 function Update () {
     if (Input.GetKeyUp ("z")) {
         phraseList.Add("Test string 1");
         phraseList.Add("Test string 2");
         phraseList.Add("Test string 3");
         
     }
 
 }



C# (solution provided by syclamoth):


 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Bluh : MonoBehaviour {
 
 List<String> phraseList = new List<String>();
 
 void OnGUI() {
     int i = 0;
     foreach(String str in phraseList)
     {
        if(GUI.Button (Rect (100, 20 * i, 200, 20), str)) {
          Debug.Log("Box #" + i + " is working");
        }
        i++;
     }
 }
 void Update () {
     if(Input.GetKeyUp("z"))
     {
        phraseList.Add("Test string 1");
        phraseList.Add("Test string 2");
        phraseList.Add("Test string 3");
        foreach(String str in phraseList)
        {
          Debug.Log(str);
        }
     }
 }
 }



Thank you both very much!

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
2

Answer by syclamoth · Sep 14, 2011 at 09:02 AM

You're on the right track here, but I don't quite know if the syntax is right. Personally, I work in C# exclusively (I find javascript just unendingly frustrating because it lacks this kind of functionality)- As far as I can tell, Javascript does not support generic lists the way you want them. I would recommend porting your script to C#- it doesn't take very long, and learning to program in it will be worthwhile in the long-term as well. For the script you posted there, the translation would be somthing along the lines of:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class Bluh : MonoBehaviour {

 List<String> phraseList = new List<String>();
 
 void OnGUI() {
     int i = 0;
     foreach(String str in phraseList)
     {
         if(GUI.Button (Rect (100, 20 * i, 200, 20), str)) {
             Debug.Log("Box #" + i + " is working");
         }
         i++;
     }
 }
 void Update () {
     if(Input.GetKeyUp("z"))
     {
         phraseList.Add("Test string 1");
         phraseList.Add("Test string 2");
         phraseList.Add("Test string 3");
         foreach(String str in phraseList)
         {
             Debug.Log(str);
         }
     }
 }
 }
Comment
Add comment · Show 8 · 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 Story Specialist · Sep 14, 2011 at 09:48 AM 0
Share

Thanks for your answer! However, as a beginner, I've really learned everything in Javascript, and I don't know if I want to switch to C# just yet. That said, your code re$$anonymous$$ded me of the following code I used for Arrays:

for (var i : int = 0; i < tempArray.length; i++) {

     //Iterate through array and print array content as buttons
     if (GUI.Button (Rect (100, 20 * i, 200, 20), "String: " + tempArray[i])) {
         //tempArray.Remove[i];
     }
     

}

Can something like that be adopted for the Generic List?

avatar image Story Specialist · Sep 14, 2011 at 09:53 AM 0
Share

Or maybe this is of use, I don't really understand it myself, but hey... :P http://forum.unity3d.com/threads/79760-How-to-use-generics-in-unity-javascript

avatar image syclamoth · Sep 14, 2011 at 10:05 AM 0
Share

I still say you are making things unnecessarily difficult for yourself by denying yourself all the useful features of C#, but that post provides a functional implementation of generics in js.

avatar image Story Specialist · Sep 14, 2011 at 03:40 PM 0
Share

$$anonymous$$aybe I will switch to C# then. Thanks for your help, in any case. I'll convert my script to C# and see if works. (Why aren't there notifications for comments?)

avatar image Eric5h5 · Sep 14, 2011 at 04:20 PM 1
Share

@syclamoth: JS doesn't lack that kind of functionality at all. The script would identical in JS, substituting JS syntax of course. i.e., ins$$anonymous$$d of foreach, use for (str in phraseList). In most cases there's no reason to use C# unless you happen to prefer it.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Iterate through Generic Dictionary? 3 Answers

ArgumentOutOfRangeException - Generic List - Javascript 1 Answer

How can I parent each item from one array to each item in another array? 1 Answer

Loop Through Entire Array - Simon Game 1 Answer

Setting Scroll View Width GUILayout 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