Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Svenq · Sep 11, 2016 at 03:42 PM · c#guiintensityrgb

C# GUI creates double lamps

Hello,

I am trying to create 3 kind of lamps and 2 of them you can control in a GUI.

  • The point light you can change the color by rgb values.

  • The directional light you can change the intensity of.

But when I run my code it creates 2 times each lamp and also 2 times the GUI

         GameObject cube;
         GameObject capsule;
         GameObject sphere;
         GameObject plane;
     
         GameObject pointLightGameObject;
         GameObject spotLightGameObject;
         GameObject diLightGameObject;
     
         Light diLightComp;
         Light pointLightComp;
         Light spotLightComp;
     
         float timeCounter = 0;
     
         // Use this for initialization
         void Start () {
     
             //Lights
     
             //Directional Light
             diLightGameObject = new GameObject("Directional Light");
             diLightComp = diLightGameObject.AddComponent<Light>();
             .....
     
     
             //Point light
             pointLightGameObject = new GameObject("point Light");
             pointLightComp = pointLightGameObject.AddComponent<Light>();
             ....
     
             //Spot Light
             spotLightGameObject = new GameObject("spot Light");
             spotLightComp = spotLightGameObject.AddComponent<Light>();
             ....
     
         }
         
         // Update is called once per frame
         void Update () {
     
             timeCounter += Time.deltaTime;
     
             float x = Mathf.Cos(timeCounter) - 1f;
             float y = 0;
             float z = Mathf.Sin(timeCounter) + 1f;
     
             pointLightGameObject.transform.position = new Vector3(x, y, z);
     
         }
     
         float r = 1f;
         float g = 0f;
         float b = 1f;
     
         void OnGUI()
         {
     
             GUI.Box(new Rect(10, 10, 140, 100), "RGB point light - changer");
             diLightComp.intensity = GUI.HorizontalSlider(new Rect(20, 100, 100, 30), diLightComp.intensity, 0.0f, 10.0f);
     
             r = GUI.HorizontalSlider(new Rect(20, 40, 100, 30), r, 0.0f, 1.0f);
             g = GUI.HorizontalSlider(new Rect(20, 60, 100, 30), g, 0.0f, 1.0f);
             b = GUI.HorizontalSlider(new Rect(20, 80, 100, 30), b, 0.0f, 1.0f);
     
             pointLightComp.color = new Color(r, g, b);
         }
 

What is the reason of this and how can i fix this problem ?

Thanks in advance. Sven

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ArturoSR · Sep 11, 2016 at 11:02 PM

@Svenq Hello there.

OK, as I can see you are not defining the type of light (or you are not showing it on your snippet), what are you just doing it's create new lights with the default type of light (in this case point light), you need to doing it like this:

alt text

And about the double generation, I can't see it in your snippet, the only thing I see it's where you control the color from one point light, maybe you need to use the for or foreach loop to change the color from 'n' point lights in your scene using only one UI slider's group, cheers.


lights.png (106.9 kB)
Comment
Add comment · Show 4 · 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 Svenq · Sep 12, 2016 at 02:01 PM 0
Share

The following was cut for each light, because else the snippet would be to long and thought this was irrelevant information. diLightComp.color = new Color(0.580392f, 0f, 0.827451f); diLightComp.type = LightType.Directional; diLightComp.intensity = 5; diLightGameObject.transform.position = new Vector3(-0.5f, 0.35f, -3.5f);

The GUI is only needed for the point light, since there is only 1 point light there is no need for a for loop.

Before running my code the scene looks as following:

Start project

This is how I get the scene after running the code (moved the lamps so you can see the duplicates):

Running Code

As you can see there are 2 lamps of each kind. Don't know where they come from myself. If i delete the duplicates, everything works fine. The only thing that then shows up is that there is missing a reference.

The GUI is double as well I just found out.

doublelampstart.png (10.4 kB)
doublelamps.png (94.0 kB)
avatar image ArturoSR Svenq · Sep 12, 2016 at 03:16 PM 0
Share

O$$anonymous$$, then, which element is holding the component?, are you absolutely sure that you are not making an instance from it too?, because, as you can see I only generate 3 lights with my code and nothing else, I define two point lights, but could be only one, just like yours.

avatar image Svenq ArturoSR · Sep 12, 2016 at 04:35 PM 0
Share

At this moment I removed the variables for each Light itself. So now the component is added immidiately to the GameObject (like in your code). This means I only work with the gameobject itself anymore, which makes the code more understandable. But this fix didn't work.

I also tried to run your code, but the problem with this is that it will create an uncountable amount of Lights because the foreach will be done every update.

Show more comments

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

248 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Locate _MainTex from a public shader and assign to instantiated prefab for GUI 1 Answer

Editor Window 1 Answer

Toggle Button: hope it toggle once but it toggle many times 1 Answer

How to fill a bar constantly between two variables? 1 Answer

Update and Get PlayerPrefs during runtime? 0 Answers


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