- Home /
Light intensity null reference
Hello, I added a light component to a muzzle flash and in the script i added an intensity modifier. It actually works great and looks nice, not being on all the time and it only appears with the muzzle flash, but i am getting a null reference still, i am not sure why...i tried adding a point light instead to the parent objects but that dint kill the call for null...so i am thinking its a small variance in the specifics of the code at the light intensity call...Can anyone help me see whats up here please?hmmm The code is in the late update
function LateUpdate() {
if (muzzleFlash) { // We shot this frame, enable the muzzle flash
if (m_LastFrameShot == Time.frameCount) {
muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 90, Vector3.forward);
muzzleFlash.enabled = true;
//lightGameObject.AddComponent(Light);
muzzleFlash.light.intensity = 3;
} else { // no shot, disable the muzzle flash
muzzleFlash.enabled = false;
enabled = false;
muzzleFlash.light.intensity = 0;
if (bulletsLeft == 0){
Reload();}
}
}
}
here is the error message...
$$anonymous$$issingComponentException: There is no 'Light' attached to the "muzzleFlash" game object, but a script is trying to access it. You probably need to add a Light to the game object "muzzleFlash". Or your script needs to check if the component is attached before using it. at (wrapper managed-to-native) UnityEngine.Light:set_intensity (single)
at Weapon_Fire.LateUpdate () [0x00079] in C:\Documending\2Big4Film--3D\Unity3D\GUN_RANGE\Assets\SCRIPTS\WEAPON SCRIPTS\Weapon_Fire.js:140
(Filename: Assets/SCRIPTS/WEAPON SCRIPTS/Weapon_Fire.js Line: 140)
but there is a light attached but via component
use muzzleFlash.GetComponent("light name").intensity=3; or place full code
Answer by yogee · Oct 22, 2013 at 09:48 AM
function Start () {
// Make a game object
var muzzleFlash: GameObject = new GameObject("The Light");
// Add the light component
muzzleFlash.AddComponent(Light);
muzzleFlash.transform.position = Vector3(0, 5, 0);
}
function LateUpdate() {
if (muzzleFlash) { // We shot this frame, enable the muzzle flash
if (m_LastFrameShot == Time.frameCount) {
muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 90, Vector3.forward);
muzzleFlash.enabled = true;
muzzleFlash.light.intensity = 3.0;
} else { // no shot, disable the muzzle flash
muzzleFlash.enabled = false;
enabled = false;
muzzleFlash.light.intensity = 0;
if (bulletsLeft == 0) {
Reload();
}
}
}
}
In line 6: not lightGameObject it is muzzleFlash ok, try this
Your answer
Follow this Question
Related Questions
Incorrect Intensity value of Light component loaded from Assetbundles 0 Answers
Light Draining Script 1 Answer
[CLOSED] Change intensity of a light with script (which isn´t attached to the light object) 1 Answer
Light intensity with mouse scrollwheel 1 Answer
How to change the intensity to go down over time and to go up after colliding with an object. 1 Answer