OverlapSphere returned collider arrays

Back again with new problems :smiley:

I have some code attached to an object in my gameworld. I’ve litterally just started building my own AI for the first time and I’m finding a gap that needs filling in my knowledge. First off, the scene is simple. One cube, on layer 12 positioned close to a second object. Second object has the following code attached as a JS.

var heardist : float = 50f;
var zmask : int = 1 << 12;
var stuff : Vector3;

function Update () {

var colliders : Collider[] = Physics.OverlapSphere (transform.Position, heardist, zmask);

for (var hit in colliders) {
	if (!hit)
	if (hit.collider) {
		stuff = hit.collider.transform.position;
		Debug.DrawLine(transform.position, stuff, Color.red);
		//hit.collider.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);


I get the following errorcode:

Object refrence not set to and instance of an object

It highlights this line
var colliders : Collider = Physics.OverlapSphere (transform.Position, heardist, zmask);
in my script.

So my intention was to simply draw a line from my object to layer 12 object. But my questions are:

Where did I go wrong in this programming?

More importantly when creating a Collider array I assume. Is there a array tutorial somewhere? Because I’m at a loss as to how i’m suppose to view the information inside other than in a for-loop. Even if I use a for-loop I don’t know what information is returned, so its out of context.

Finally, In a similar application, using a var to capture the raycastall command to return a bunch of collided objects, how do I pick apart that variable, as if to say. Which object collided first? What position was it at? what rotation? which was the second object collided? The third? The last? These is all information that would be handy!

Thanks again. If you can only tackle one aspect that’s fine.
Awaiting enlightenment…


Sorry this is in C#, but I’m sure you can make it work:
using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
    public float heardist = 50f;
    public int zmask = 1 << 12;
    public Vector3 stuff;
    public float damage = 10;

    void Update()

        Collider[] colliders = Physics.OverlapSphere(transform.position, heardist, zmask);
        foreach (Collider col in colliders)
            Debug.Log("I hit: " + col.name);
            stuff = col.transform.position;
            Debug.DrawLine(transform.position, stuff, Color.red);

            col.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);