- Home /
How do I initialize a simple light in C#?
Hello everybody :)
I'm just trying to make my first little game with unity, now I'm still learning this and I'm not very used to the class architecture of this engine yet.
I'm trying to initialize some lights in my scene, using a C# script. Now I took an example codesnippet frome here, which is Javascript, and tryed to translate it into C#.
This came out:
private void init_light() {
GameObject lightGameObject = new GameObject("The Light");
// Add the light component
light = lightGameObject.AddComponent(Light);
// Set color and position
lightGameObject.light.color = Color.blue;
// Set the position (or any transform property) after
// adding the light component.
lightGameObject.transform.position = new Vector3(0, 5, 0);
}
Now in line 5 the Compiler gives me this Error:
error CS0119: Expression denotes a
type', where a
variable',value' or
method group' was expected
I also tried to change the AddComponent(Light) to AddComponent(new Light()), but then i get this:
Assets/main/main.cs(29,41): error CS1502: The best overloaded method match for `UnityEngine.GameObject.AddComponent(string)' has some invalid arguments
When i change Light to "light", i don't get any lightobject.
Can anybody tell me how to do it? I'd also like know why to use Javascript anyways, isn't C# faster?
Answer by robertbu · Dec 14, 2013 at 03:36 PM
Go by to the reference at the link you provided. In the upper left corner see the string and drop down 'Scripting Reference Using'. Change the dropdown to C#. The sample code will change to C#. As for your AddComponent():
lightGameObject.AddComponent<Light>();
Thanks a lot, that worked out for me. :)
I didn't see the dropdown yet, I was wondering why all the documentation is written in JavaScript, thanks for that too haha
Answer by W.Walter · Dec 15, 2013 at 12:43 AM
or add a cast;
light = (Light)lightGameObject.AddComponent(Light); By the way you are declareing the light variable??
ie: Light light = (Light)lightGameObject.AddComponent(Light);
Your answer
![](https://koobas.hobune.stream/wayback/20220613124143im_/https://answers.unity.com/themes/thub/images/avi.jpg)