ive tried and tried to do this but i am not a programmer im trying to get a battery pickup to recharge my flashlight but nothing is working this is the flashlight script
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Light), typeof(AudioSource))]
public class Flashlight : MonoBehaviour {
public AudioClip clickSound;
public float batteryLifeInSec = 300f;
public float batteryPercentage = 100;
private bool on;
private float timer;
void Update() {
Light lite = GetComponent<Light>();
timer += Time.deltaTime;
if(Input.GetKeyDown(KeyCode.F) && timer >= 0.3f && batteryPercentage > 0) {
on = !on;
audio.PlayOneShot(clickSound);
timer = 0;
}
if(on) {
lite.enabled = true;
batteryPercentage -= Time.deltaTime * (100 / batteryLifeInSec);
}
else {
lite.enabled = false;
}
batteryPercentage = Mathf.Clamp(batteryPercentage, 0, 100);
if(batteryPercentage == 0) {
lite.intensity = Mathf.Lerp(lite.intensity, 0, Time.deltaTime * 2);
}
if(batteryPercentage > 0 && batteryPercentage < 25) {
lite.intensity = Mathf.Lerp(lite.intensity, 0.5f, Time.deltaTime);
}
if(batteryPercentage > 25 && batteryPercentage < 75) {
lite.intensity = Mathf.Lerp(lite.intensity, 0.8f, Time.deltaTime);
}
if(batteryPercentage > 75 && batteryPercentage <= 100) {
lite.intensity = Mathf.Lerp(lite.intensity, 1, Time.deltaTime);
}
}
}
I have this on s spotlight under the main camera i cant get a separate script to read form this one so when i pick up the battery it adds batteryPercentage i realy appreciate your help
Update 1 :
Would this work? cant test not at my pc
using UnityEngine;
using System.Collections;
public class Battery : MonoBehaviour {
public Battery_pickup Battery_pickup;
void OnTriggerEnter(Collider other){
void Update () {
//pick up
void OnTriggerEnter(Collider other){
if(other.gameObject.tag == "Pickup") {
//add Charge
Destroy(other.gameObject);
Flashlight.batteryPercentage += 10;
}
}
using UnityEngine;
using System.Collections;
Update 2 :
public class Battery : MonoBehaviour {
public Battery_pickup Battery_pickup;
void OnTriggerEnter(Collider other){
void Update () {
//pick up
if(other.gameObject.tag == "Flashlight") {
//add Charge
Destroy(other.gameObject);
Flashlight.batteryPercentage += 10;
}
}