- Home /
Find index number in an Array of a transform by it's name
Hi,
I have an array which is populated with transforms like this:
var attackerClonesArray = new List.<Transform>();
I am trying to find the index number of a transform in the array by searching for it's name.
I am getting the transform name like this:
var thisTransformName : String;
thisTransformName = transform.name;
My goal is to then erase that transform from the array. I've tried an number of solutions such as index and other stuff but can't seem to quite get it.
Any suggestions?
Thanks
Paul
untested theory
var myPlaceInArray : int = -1;
for (var i:int = 0; i < attackerClonesArray.length; i ++)
{
if ( attackerClonesArray[i] == thisTransformName )
{
attackerClonesArray[i] = null;
myPlaceInArray = i;
}
}
Thanks Alucardj! Will give this a go if I have no success with Fattie's suggestion, but it looks good so this gets a like ;)
No worries, thankyou. If you want to use dynamic lists (as Fattie states, never use dynamic arrays) absolutely go with the answer (I would!).
However I would like to make a case for static arrays. I did some research a while back and found that Static arrays are infact more efficient than List. Surprising but yes. If there are any other considerations with using arrays in unity I am not aware of, then I am certainly open to advice.
dude -- if you mean absolutely plain arrays (like same as arrays in c .. a block of memory), yes that is 18 million billion times more efficient
that's why they are used for the mesh of course, for instance.
but "javascript arrays" are toss, never use them
here !
http://answers.unity3d.com/questions/198318/javascript-array-use-with-a-struct-.html
You got me. $$anonymous$$y research was purely derived from using static arrays for meshes. As I mentioned above, I would absolutely not even consider using a dynamic array, but havn't had the need for dynamic Lists much as yet, so am still very green when it comes to the commands available there. A couple of times I have forgotten to include import System.Collections.Generic; which led to headaches for I did not understand. That was an excellent link, thanks.
Answer by Fattie · Oct 03, 2012 at 08:24 PM
do NOT use arrays. use List.
never, ever use arrays in Unity.
as well, once you do use List, you have all these available to you ...
http://msdn.microsoft.com/en-us/library/d9hw1as6(v=vs.90).aspx
you can do it with a couple lines of code
for sure, it is central to unityscript
(that link is just a handy reference for the many List commands.)
suggest you post a question "please explain how to use List to me"
import System.Collections.Generic;
bb = new List.();
bb.Add( something )
bb[2].position
bb.RemoveAt( 3 )
sort, reverse, find, .. heaven
it's incredibly easy. never use arrays.
plus they are much faster etc
there must be a godo list tutorial somewhere ask a questino
please tick and vote me here :-)
This looks great. Looks like I'm investigating List tonight ;) Thanks for the advice Fattie.
don't put any effort in to it! just ask here and someone else will do all the work man! there must be a great tute out there someone here will know.
it would be the best question on the site for a week. cheers
O$$anonymous$$ people. Fattie was right. Lists rock. Here is a continuation of the subject here: http://answers.unity3d.com/questions/327065/explanation-on-how-to-use-list-in-unityscript.html
Your answer
Follow this Question
Related Questions
Array of transform index out of range error 1 Answer
Explanation on how to use LIST<> in UnityScript 2 Answers
IndexOutOfRangeExeption - Array index is out of range 2 Answers
EditorGUILayout Array 1 Answer
move enemy to random transform array 2 Answers