Searching through an Array

Hi, I am trying to search through my objects to create another array comtianing just the objects called ‘waypoint01’, ‘waypoint02’ etc When I use this:

function Start ()
	var InRange : Collider[] = Physics.OverlapSphere(transform.position, 10);
		for (var i = 0; i < InRange.length; i++){
  • }*

  • for (n=0; n < InRange.Length; n++){*

  •  if (InRange[n] == "waypoint03")*
  •  	Debug.Log("You found waypoint03!");*
  •  	}*

var search = function(Waypoint01){

  • for(var i in InRange){*
    _ if(InRange == Waypoint01){_
    _ Debug.Log(InRange*);

Unity then complains that:
Assets/scripts/overlapsphere.js(16,9): BCE0022: Cannot convert ‘UnityEngine.Collider’ to ‘int’.
So can anyone tell me how I can convert it to ‘int’ or whether I am just going about this all wrong.

You’re trying to use a foreach loop as though it is a regular for loop. The foreach loop (in Javascript, this looks like for(var i in Array)) does not use the index of the array as its argument, but the value in the array. When you write for(var i in InRange){, the variable i is not assigned to the index in turn (i.e. 0,1,2… etc), but the value (i.e. InRange[0], InRange[1]… etc). This means that when you write InRange* on the next line, you are essentially writing: InRange[InRange[0]], which throws a type error because you can’t index an array with a Collider. To further demonstrate this, I will convert your last loop into the other kind of for loop, to demonstrate what it is trying to do:*
var search = function(Waypoint01)
for(var i = 0; i < InRange.length; i++)
if(InRange[InRange*] == Waypoint01)*
This code will execute in almost exactly the same way as yours, and will throw the same error. I hope you can see the problem with it.
No, absolutely not. You’ve gone worse, if anything, because it’s now not even syntactically correct.
Let me teach you the basics of a for loop. A for loop is very similar to a while loop (in fact, under the hood, all for loops are while loops), but with the difference that they retain some information between passes of the loop. The structure of a for loop is like this:
for(assign variable starting value; conditions under which to continue; action to perform after each pass)
The most common for loop is the counting loop, which is usually written like so:
for(var i = 0; i < 10; i++)
So, reading this out, it says that, before starting the loop, we create a variable, to which we assign the value 0. Then, we test the conditions - is the variable i less than 10? Since i is 0, this returns true, and we execute the loop. When the end of the pass is finished, it runs the actions (i++), which adds one to the variable i, and starts again (from the point where we tested the conditions). This in itself is pretty useless, but since the conditions are arbitrary, you can substitute a variable in for the 10. Most commonly, this is the length of an array.
Now, you need to take into account types here. Javascript is not a strongly typed language (and actually, this makes it bad for beginners, because it can make for some very confusing code), but here it’s pretty clear that i is of type int. When indexing arrays, you use an integer value, so this is fine for indexing into arrays, in the same way you would write Array[0], writing Array_ is perfectly valid. Remember, the variable defined in a for loop is not special, it behaves just like any other variable.
So, let us take an example of operating on an array called array, containing the elements: 10, 9, 8, 7 and 6.
for(var i = 0; i < array.Length; i++)
Debug.Log(i + " : " + array*);*
This will print out:
0 : 10
1 : 9
2 : 8
3 : 7
4 : 6
Note that i is the position of the element in the array, while array is the actual element at that position (also, it’s worth noting that arrays are 0-indexed, meaning the first element is at index 0).
Now, let us talk about the foreach loop. This is actually just an abstraction for a specific kind of for loop. Foreach loops are written like this:
for(new variable in array)
This allows the loop to iterate through an array, returning the value of the array as the variable of the loop. This is essentially the same as the previous example, but now we can’t easily access the index of the element we’re looking at - we just get the actual element, so we can’t use this for counting to arbitrary numbers any more, but we can use it to shorten our previous example:
for(var t in array)
(Note that I’ve called the variable t here, not i. The name is not actually important, but by convention, i is reserved for the index of an array, and that is not what we’re looking at). This will print out, given the same array as before:
Note that this time, even through we’re printing out the variable directly from the loop, we get the elements of the array, not the indices. Thus, the type of t here is not necessarily int. The type of this variable depends on the type of the array. In this example, I had an array of ints, so the type of t would also be int, but if I replaced it with an array of floats, then the type of t would be float.
Now, I want to say one last thing on foreach loops. I mentioned that they were an abstraction, well, that’s because they are actually just a shorthand way of writing this (to use the above loop for comparison):
for(var i = 0; i < array.Length; i++)
var t = array*;*
This is actually identical to the foreach loop I wrote above (and hopefully you can start to see why I didn’t call the temporary variable i).
So, key points to take away:
- for loops are designed to loop and hold the state of a variable while
they do.
- foreach loops are specifically designed for iterating through arrays
and collections.
- The variables defined in for loops are not special - they are the same
as any other variable.
- The syntax for using these variables is the same as any other variable._