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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by code-blep · Oct 03, 2012 at 09:19 PM · listnameindexsearch

Explanation on how to use LIST<> in UnityScript

Hi,

I'm on a mission to find the index number in a List of a transform by it's name.

I was previously using Arrays but I have been pointed in the direction of Lists. From what I can tell they are like Arrays but with lots of cool built in functions. There is a tiny performance hit from what I can tell, but apparently nothing to worry about in smaller projects. Here is an excellent reason why to use Lists instead of Array:

http://answers.unity3d.com/questions/198318/javascript-array-use-with-a-struct-.html

So they sound great but I'm having problems getting to grips with it. So far I have found out how to get the name of an item from it's index array. But I can't get it to work the other way around, and give me the index number by searching the name of the item in the list. Here is an example of my code so far:

 import System.Collections.Generic;

 var attackerList = new List.< Transform>()
 //attackerList list populate here
 
 function Start () {
 var thisItem = attackerList[2];
 print ("Attacker name is=" + thisItem);
 }

So does anyone know of any good guides out there? And any suggestions on how to jump the final hurdle ;)

Thanks!

Paul

[1]: http://answers.unity3d.com/questions/198318/javascript-array-use-with-a-struct-.html

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

Answer by Eric5h5 · Oct 03, 2012 at 09:58 PM

Use List.FindIndex:

 import System.Collections.Generic;
 
 var attackerList : List.< Transform>;
 
 function Start () {
     var index = attackerList.FindIndex (function (tr : Transform) tr.name == "TransformNameHere");
     Debug.Log (index);
 }

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 code-blep · Oct 04, 2012 at 06:45 PM 0
Share

Hi Eric5h5. That works a treat, and I've just realised that you introduced me to Lists in a previous question. I never knew they existed and missed what exactly was going on. Thanks once again, and thanks to everyone who helped me in my lists adventure! I'm going to play with them more tonight!

I'm still looking for a tutorial, even though this problem is fixed, it would be great to find a good guide. Feel free to shout out!

Paul

avatar image code-blep · Oct 04, 2012 at 07:15 PM 0
Share

I just found this which may be useful: http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F

avatar image code-blep · Oct 04, 2012 at 07:34 PM 0
Share

I am now completely sold on Lists. Now that I am able to locate the index number of my transform, I can remove it from my list with ease (which is how this whole journey started!). All you have to do is:

attackerList.RemoveAt(index);

Of course in that example I am referencing my own variables to match Eric5h5 solution above. The reference structure is: myList.RemoveAt(i);

Hope this helps others.

avatar image
1

Answer by MickM · Oct 03, 2012 at 09:47 PM

I use a basic loop in something I am working on to add an ability by its index on a master list by using the below:

 function AddAbility(skill : Ability){
     var abilityIndex = GetAbilityIndex(skill, masterAbilityList);
     if (abilityIndex != -1){
         AddAbility(abilityIndex); //This calls an AddAbility (int) method in case you were wondering
     }
 }
 
 
 
 function GetAbilityIndex(theItem : Ability, theList : AbilityList){
     for (var i = 0; i < theList.abilities.length; i++){
         if (theList.abilities[i] == theItem){
             Debug.Log("Item is at #" + i);
             return i;
         }
     }
     Debug.Log("Error Ability Index!");
     return -1;
 }

So I would assume for you, something like:

     for (var i = 0; i < attackerList.length; i++){
         if (attackerList[i] == transform){  //Current script transform -- can replace with other values if needed
             Debug.Log("Item is at #" + i);
             return i; // returns the index of the transform
         }
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 code-blep · Oct 04, 2012 at 06:46 PM 0
Share

Thanks $$anonymous$$ick$$anonymous$$! I'm going with Eric5h5 on this one but have an upvote for taking the time ;)

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

12 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

Related Questions

Find index number in an Array of a transform by it's name 1 Answer

A node in a childnode? 1 Answer

Search List by varName and get its varDescription 0 Answers

Search for an object by name null reference exception 1 Answer

Make object move through waypoints by the given number in the name 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