- Home /
Beam of light for flashlight coding issue
So I used RealityFluxStudios tutorials on YouTube to make the flashlight script. Thats all working. You pick up the flash light object. If you hit F key it turns it on. if you hit the F key again it turns it off. Sound effect for off and on. So I wanted to add a cone mesh to it that i can texture/shade later to give it a BEAM effect. I got going, messing with the code but im horrible. I got it so the BEAM and LIGHT are off at the start and when you pick up the flashlight object the LIGHT does not come on till you hit the F key, but the stupid BEAM keeps coming on as soon as i pick up the object. Im probably missing something simple here.
using UnityEngine; using System.Collections;
public class Flashlight : MonoBehaviour { public static float BatteryLife = 100; public static GameObject HeadlightMount;
public AudioClip on; public AudioClip off;
public Light HeadLight; public GameObject FlashlightCone;
public float BatteryReductionSpeed = 3.0f;
private AudioSource audioSource;
void Awake() { audioSource = GameObject.Find("headlamp").GetComponent(); HeadlightMount = GameObject.FindWithTag("Headlamp"); HeadLight.enabled = false; FlashlightCone.active = false;
}
void Update() { if (HeadLight.enabled) { BatteryLife = BatteryLife - (BatteryReductionSpeed * Time.deltaTime);
} if (Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && !HeadLight.enabled) { audioSource.clip = on; audioSource.Play();
if(BatteryLife <= 0 && HUD.BatteryCount > 0) { HUD.BatteryCount--; BatteryLife = 100; } FlashlightCone.active = true; HeadLight.enabled = true; } else if(Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && HeadLight.enabled) { audioSource.clip = off; audioSource.Play();
FlashlightCone.active = false; HeadLight.enabled = false; }
if(BatteryLife <= 0) { BatteryLife = 0; HeadLight.enabled = false; FlashlightCone.active = false; } } }
$$anonymous$$ove the comment to an answer and choose it, so this doesn't show up as unanswered, please.
Answer by MadToLove · Aug 14, 2012 at 07:47 PM
NM i found out it was because of my item pickup script, i had my BEAM childed to my headlightmount object and thats why, as soon as i unchilded it my script works like a charm!
Your answer
![](https://koobas.hobune.stream/wayback/20220613075642im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
How can i make this animation play only once? C# 1 Answer
Need help working with NGUI progress bar 2 Answers