Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dray · Dec 14, 2013 at 03:08 PM · c#lightcreateinitialization

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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>();
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dray · Dec 15, 2013 at 01:01 AM 0
Share

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

avatar image
0

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Multiple Cars not working 1 Answer

Rotate a camera when my flashlight is on the edge of the screen... Video included 2 Answers

Mesh and material disappear after CreatePrefab() in C# 1 Answer

How do you create a menu using C# in Unity? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges