- Home /
Cant turn on flashlight
When i Picked Up the FlashLight I cant Activate it but unity gives me no error I have three scripts HUD FlashLight And Pickup Here They Are can Anyone Please Help me
FlashLight.cs
public class Flashlight : MonoBehaviour
{
public static float BatteryLife = 100;
public static GameObject FlashLightMount;
public Light HeadLight;
public float BatteryReductionSpeed = 3.0f;
void Awake()
{
FlashLightMount = GameObject.FindWithTag("FlashLight");
HeadLight.enabled = false;
}
void Update()
{
if(HeadLight.enabled)
{
BatteryLife = BatteryLife - (BatteryReductionSpeed * Time.deltaTime);
}
if(Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && HeadLight.enabled)
{
if(BatteryLife <= 0 && HUD.BatteryCount > 0)
{
HUD.BatteryCount--;
BatteryLife = 100;
}
HeadLight.enabled = true;
}
else if(Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && HeadLight.enabled)
{
HeadLight.enabled = false;
}
if(BatteryLife <= 0)
{
BatteryLife = 0;
HeadLight.enabled = false;
}
}
}
HUD.cs
using UnityEngine;
using System.Collections;
public class HUD : MonoBehaviour
{
public static int BatteryCount;
public static bool HasFlashlight;
void OnGUI()
{
if(HasFlashlight)
{
GUI.Label(new Rect(10,10,80,30), "Flashlight" );
//GUI.Label(new Rect(70,10,80,30), Flashlight.BatteryLife.ToString("F2"));
}
GUI.Label(new Rect(10,40,80,30), "Batteries" );
GUI.Label(new Rect(70,40,80,30), BatteryCount.ToString());
}
}
Pickup.cs
using UnityEngine;
using System.Collections;
public class Pickup : MonoBehaviour {
public enum Item { Flashlight, Battery }
public Item item;
public AudioClip flashlightSound;
public AudioClip batterySound;
void OnTriggerEnter()
{
if(item == Item.Flashlight)
{
Flashlight.FlashLightMount.SetActiveRecursively(true);
Flashlight.FlashLightMount.active = true;
AudioSource.PlayClipAtPoint(flashlightSound, transform.position, 1);
HUD.HasFlashlight = true;
}
else
HUD.BatteryCount++;
AudioSource.PlayClipAtPoint(batterySound, transform.position, 1);
Destroy(gameObject);
}
}
Answer by ShadoX · Aug 18, 2013 at 06:56 PM
Are you sure that "HeadLight.enabled" is actually set to true? Because the code wont work if it isn't.
if(Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && HeadLight.enabled)
{
if(BatteryLife <= 0 && HUD.BatteryCount > 0)
{
HUD.BatteryCount--;
BatteryLife = 100;
}
HeadLight.enabled = true;
}
else if(Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && HeadLight.enabled)
{
HeadLight.enabled = false;
}
Both check if the HeadLight is enabled. So I guess that you wanted to check in one of them if it isn't enabled and if it isn't you do that in the first 'if'.
I guess that you could try adding a ! in the first if ...
if(Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && !HeadLight.enabled)
Your answer
Follow this Question
Related Questions
Android: Application.LoadLevel("level_name") does not do anything 1 Answer
Counting colliders 2 Answers
Error with trigger PLZ HELP!!!! 1 Answer
(Solved) Collision happens, but is not detected? 1 Answer
Javascript error.. insert semicolon?? 2 Answers