hi quick question i do believe that i shouldnt use the update or fixedupdate fuction because that calls the component ever frame is that true?
also can you tell me what i should use instead if any at all
hi quick question i do believe that i shouldnt use the update or fixedupdate fuction because that calls the component ever frame is that true?
also can you tell me what i should use instead if any at all
Do you mean GetComponent<>() and its siblings? It’s usually better to call those in Awake and then save the results to variables, so you can continue to reference the components as often as you need to but without having to search for them constantly.
More generally, if you have something that needs to happen frequently, then Update or FixedUpdate are probably good places to do it. If it only needs to happen once, you probably want to put it in Awake, Start, or maybe OnEnable. If it happens in response to a specific thing happening, you may want to use events.
so is this ok to have update method then… heres my script
Copyright of brayden roberts aka brayT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class flashlight : MonoBehaviour
{
public LightType LightType;
public Light FlashLight;
public float LightDistance = 3;
public float lightIntensity = 1;
public Texture FlashLightTexture;
public Color FlashLightColors;
public LightRenderMode FlashLightRenderMode;
// Start is called before the first frame update
void Start()
{
FlashLight = GetComponent<Light>();
FlashLight.GetComponent<Light>().enabled = false;
//light quality setting... this is the settings on start
FlashLight.GetComponent<Light>().range = LightDistance;
FlashLight.GetComponent<Light>().intensity = lightIntensity;
FlashLight.GetComponent<Light>().cookie = FlashLightTexture;
FlashLight.GetComponent<Light>().color = FlashLightColors;
FlashLight.GetComponent<Light>().renderMode = FlashLightRenderMode;
FlashLight.GetComponent<Light>().type = LightType;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Z))
{
FlashLight.GetComponent<Light>().enabled = true;
}
else
if(Input.GetKeyDown(KeyCode.X))
{
FlashLight.GetComponent<Light>().enabled = false;
}
//light quality settings... this is where i can modify in the inspector while game is running
FlashLight.GetComponent<Light>().range = LightDistance;
FlashLight.GetComponent<Light>().intensity = lightIntensity;
FlashLight.GetComponent<Light>().cookie = FlashLightTexture;
FlashLight.GetComponent<Light>().color = FlashLightColors;
FlashLight.GetComponent<Light>().renderMode = FlashLightRenderMode;
FlashLight.GetComponent<Light>().type = LightType;
}
}
FlashLight is already a Light. GetComponent() will just return the same object that is already assigned to FlashLight.
So replace things like
FlashLight.GetComponent<Light>().enabled = false;
with
FlashLight.enabled = false;
What bobisgod234 said. The whole point of assigning it to a variable is so that you don’t have to keep calling GetComponent over and over.
k Thanks
I’d also think about that part as well.
First of all, if you only need to tweak these values through the inspector, it’s probably better to just edit the light component directly. After all, these fields do not represent anything else than the corresponding fields that are already exposed on light components.
Secondly, if you ever need to update stuff that’s tweakable in the inspector and should be “updated” in terms of assignment to the target components as soon as possible, you may be better off using Unity’s Editor functionality, i.e. OnValidate.
Reason being is that otherwise, this code might make it into the build version and you’d be wasting a tiny piece of performance per component instance for literally no reasonable effect. That’s not noticeable for a few components, but it can add up if this sort of inspector value re-assigment in Update becomes a habit in your code base.
OnValidate would limit these assignments to the editor, and the programmatic assignment would be “on demand” through an appropriate setter, so that it doesn’t happen every frame (unless you call the setter every frame).
That’s overkill, if it was ME I would do this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
Light flashlight;
void Awake()
{
flashlight = GetComponent<Light>();
flashlight.intensity = 1;
flashlight.range = 15;
}
Then continue in that fashion. IF it needs to be edited via script. Else just set in the inspector. Afterwards you can continue on to writing the rest of the code.