Creating an object at another object's location

This is probably a dumb question, but I am just getting into Unity and need all the help I can get! :slight_smile:

I have a lightswitch that I want to make a sphere appear at when it is triggered. The code I have thus far is:

var lightObject = GameObject.Find("LightCube");

function OnCollisionEnter(collision : Collision) {
	light.enabled = !light.enabled;
  
  if(light.enabled){
	 var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
	 sphere.transform.position = lightObject.transform.position;
	}
	
}

However, when the sphere is created, it is made at coordinates 0,0,0, rather than the coordinates of the light. What exactly am I doing wrong?

Thanks in advance!

You can not declare a variable outside of a function so this needs to be done.

var lightObject = GameObject.Find("LightCube"); // Change this to var lightObject: GameObject;

function OnCollisionEnter(collision : Collision) {
    // Put this line in here. lightObject = gameObject.Find("LightCube");
/*
GameObject and gameObject are two different things. A GameObject is (I forgot the word for it) a specifier of the variable. Like for example, var pi: float; It it saying this can only be filled by a number and this number can have decimal points.

gameObject is talking about the object that can be found in the scene at that point so gameObject is referring to the scene and what is in it. So please be careful at this. It took me like a day or two to understand how it works. I maybe started using unity a month ago and I am starting to be able to answer basic questions such as these.
*/
    light.enabled = !light.enabled;

  if(light.enabled){
     var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
     sphere.transform.position = lightObject.transform.position;
    }

}