proximity detection for noobs

Hi,

I’m a complete noob to Unity.

What I am trying to do:

I have a room defined by some walls. Inside the room is a cylinder. I’m using the first person pre-fab.

What I want to happen is that when my first person player gets close to the cylinder, a spotlight shines on the cylinder.

For the life of me, using scripts to try to do this, all I get are errors about the objects I’m trying to use or call are null or don’t exist.

I guess I don’t have a concept yet of the object structure of Unity.

Maybe there is a way to do something this simple in the Unity GUI and I don’t even need a script?

Thanks!

The first thing you need is to hook up the light, the transform of the cylinder, and the transform of the player. This can be done on any of the three, but attaching the script to the light would probably be the cleanest.

var cylinder : Transform;
var player : Transform;
//and the light is accessible with "light"

Put that into a script, save it, and attach the script to the light. You’ll see two “slots” on the script; drag the cylinder and player objects into the appropriate slots.

Then all you need to do is check whether the distance between them is less than a certain value.

var distanceLimit : float = 5.0;

function Update() {
if (Vector3.Distance(cylinder.position, player.position) < distanceLimit) {
light.enabled = true;
}
else {
light.enabled = false;
}
}

I did this and it worked great!

Thanks!