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
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:
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.
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:
This is how I get the scene after running the code (moved the lamps so you can see the duplicates):
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.
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.
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.