# Making a Point Light follow a cube (Unity)

I'm trying to have a point light positioned directly above a cube follow wherever the cube goes. However, it doesn't move at all. This is my code for the light:

``````using UnityEngine;
using System.Collections;

public class SpotlightController : MonoBehaviour {

// Use this for initialization
void Start() {

}

void OnGUI(){
GUI.Label(new Rect(20,50,500,20), "(" + transform.position.x.ToString() + ", " + transform.position.y.ToString() + ", " + transform.position.z + ")");
}

// Update is called once per frame
void Update() {
GameObject cube = GameObject.Find("Cube");
float xlocation = cube.transform.position.x;
float zlocation = cube.transform.position.z;

transform.TransformPoint(new Vector3(xlocation,transform.position.y, zlocation) * Time.smoothDeltaTime);
//transform.Translate(new Vector3(xlocation, 0, zlocation));
}
}
``````
-

## 2 Answers

This is untested code, but I think it should be as simple as this:

``````void Update() {
GameObject cube = GameObject.Find("Cube");
transform.position = cube.transform.position;
}
``````
-
I do believe this makes the light go inside the cube, instead of being above it. – Esa Apr 24 '12 at 5:10
Yup. You can build a new Vector3 out of the cube's transform.position, increment the y value, and then assign the modified vector back to the light's transform.position. – Kylotan Apr 24 '12 at 10:15
If you really, really must use GameObject.Find (and you probably don't), then GameObject.Find(string) should be cached to a variable inside Start() and Update() should use the variable. – mghicks Nov 25 '12 at 16:17
Yeah, this code is not supposed to be optimal, but is showing how to make the original poster's code work in the simplest way. He might have a good reason for looking up the target each frame (but probably not!) Bit harsh to get a downvote though - the answer here is useful even if it's not optimal! – Kylotan Nov 26 '12 at 15:54
Fair point. My comment really belongs on the OP. I'd take back the downvote, but it doesn't seem like I can. – mghicks Nov 30 '12 at 17:22

You can also do it without code. Attach the point light to your Cube object and set its relative position.

-
This seems like a much better answer than the accepted one. – mghicks Nov 25 '12 at 16:14
+1, as a side note: this won't work if the light has got a Rigidbody component. (I once put one on it by accident) – ComFreek Jan 24 '14 at 17:09