- Home /
How to change a point light intensity with an updating float
For reference, speedMultiplier = 200 and the intensity fo the point light is 5000. (lux) My engine object has a point light child and this script. Speed Multiplier is the speed of another object, which I want to use to control the light intensity. multiplierUpdate defines and displays the float values, while UpdateValues overrides the light.intensity. When I start the game, the point light disappears from the inspector, and lightIntensity says 318 for some reason. When I delete UpdateValues to see what happens, the point light stays in the inspector when I start, but lightIntensity still says 318, clearly referring to something else.
public class Engine_Controller : MonoBehaviour
{
public Free_Roam freeRoam;
public Light lightObject;
public float lightIntensity;
public float speedMultiplier;
public void Update()
{
multiplierUpdate();
UpdateValues();
}
public void multiplierUpdate()
{
lightIntensity = lightObject.intensity;
speedMultiplier = freeRoam.speed;
}
void UpdateValues()
{
lightObject = gameObject.GetComponent<Light>();
lightObject.intensity = lightIntensity * speedMultiplier;
}
}
Answer by toficofi · Mar 21, 2021 at 01:04 PM
You are setting lightIntensity = lightObject.intensity
and then assigning it back to lightObject.intensity = lightIntensity * speedMultiplier
. This means that on the next update, you are doing lightIntensity = lightObject.intensity
again which gives you the already multiplied value, and you multiply it again, and so on.
You probably just want to remove lightIntensity = lightObject.intensity
and instead do lightObject.intensity = lightObject.intensity * speedMultiplier
.
also - you say "My engine object has a point light child and this script", but gameObject.GetComponent<Light>
doesn't look for children, it looks for components attached to that game object.
Yup! I understand the disconnect now, a very simple misreading of some basic steps. I love learning this stuff but the price is a lot of hit and miss, lol. Anyway, thanks for your input! for reference, it looks like this now:
public class Engine_Controller : MonoBehaviour
{
public Free_Roam freeRoam;
public Light lightObject;
public float lightIntensity;
public float speedMultiplier;
public void Start()
{
speedMultiplier = freeRoam.speed;
lightIntensity = lightObject.intensity;
}
public void Update()
{
speedMultiplier = freeRoam.speed;
UpdateValues();
}
void UpdateValues()
{
lightObject.intensity = lightIntensity * (speedMultiplier / 100);
}
}