Hi I’m fairly new to Unity development and came across an issue where Unity seems to not be recognising the Light component in my GameObjects. I am trying to write a script to disable the light from one of my point lights when a certain condition is met. The object containing the light is as follows:
and the script that is trying to disable the light looks like this in the inspector:
The code snippet which is causing my issues is as follows:
private IEnumerator Burn()
{
Light l = lightSource.GetComponent<Light>();
while (fuelLeft >= 0)
{
if (fuelLeft <= fuelCapacity / 2)
{
sr.sprite = half;
if (fuelLeft <= fuelCapacity / 4)
{
sr.sprite = quarter;
if (fuelLeft == 0)
{
ps.Stop();
l.enabled = false;
//error: NullReferenceException: Object reference not set to an instance of an object
sr.sprite = gone;
}
}
}
fuelLeft = fuelLeft - fuelDraw;
yield return new WaitForSeconds(3);
}
}
Unity does not seem to be able to access the Light component of the Point Light game object and I have no idea what to do about it. I have tried using the same script with a point light without a particle system because I thought that might be the issue but I came across the same error. Any help would be greatly appreciated.
Is the “lightSource” variable declared as a GameObject? Why not directly declare it as a Light? You can avoid the GetComponent call entirely.
Are you 100% sure the Point Light object in the Fire Behaviour’s inspector is the same object from the first screenshot? Are there any prefabs involved here?
Are you 100% sure it’s “l” that’s null? It’s definitely not “ps”?
Everything that @PraetorBlue says is spot on, and I want to add, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run?
what are the values of the variables involved? Are they initialized?
Knowing this information will help you reason about the behavior you are seeing.
You can also print the name field of lightSource, which will help you reason about whether you are operating on the correct GameObject.
Hi, I probably should have mentioned that I tried declaring it as Light and Unity would not let me drag and drop any Light components into the slot. The reason it is declared as GameObject in the screenshots is because I use its ParticleSystem too, so I thought it would make more sense to only drag it once into the inspector.
EDIT: Just reread your reply and thought I should clarify that it runs fine without ‘l’ so I’m sure that is the issue and not ps
Hi, Yes the code runs. I have put debug logs in and it does reach that point and the time at which it reaches it is when the error is displayed (for clarification, I mean the line at ‘l.enabled = true’). It only reaches it once, but the while loop runs 10 times before that happens. All variables are initialised - I’m really at a loss about why this error is occurring.
@Kurt-Dekker@PraetorBlue
Turns out I was stupid and named a different script ‘Light’ without it occurring to me that the Light component already existed so when Unity was trying to get the Light component it was actually looking for that script called ‘Light’ rather than the Light component I intended.
Thank you both for trying to help me out, it is much appreciated