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

@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:

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.