- Home /
Rechargeble flashlight with battery pick up
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;
}
}
you do realize you are program$$anonymous$$g right?
well there are 2 ways either stop program$$anonymous$$g or start thinking you want to be a programmer.
You could simplify this using the batteryPercentage for the light intensity, that would avoid all the if statements and your light would linearly fall off ins$$anonymous$$d of gradually.
Also, when you pick up battery, the battery percentage gets increased and automatically your light goes back up.
Now your actual question is not at all related to that code. You are after the answered 100 times GetComponent issue.
So you need to have a OnTriggerEnter method that detects when you hit the pick up item, then using GetComponent you can access that script and modify the value.
To:sdgd I'm learning to program stuff on the go when it comes to reading it I can understand it but when writing it from scratch no matter how small I understand Spanish but I can't speak it
To:fafase I've tried to use get component the script always has errors with the get component line and would it be hard use a button to pick up ins$$anonymous$$d of a collide trigger any axamples? And I have a light flicker script that is at line 39 but I left it out if I were to simplify it can I keep lines 37 through 40?
It doesn't work i get the same error i always get $$anonymous$$ey word void can not be used in this context......
You get "key word void" because you are creating a new function inside your Update function. you dont want to do that.
for this issue, move the OnTriggerEnter function outside of the { } of the Update function.
Also, just judging from the names of your tags and variables. It looks like you are putting this particular OnTriggerEnter function on your battery and it checks to see if the "other" is a "PickUp". Battery should check to see if other is a Player or a Flashlight... Player or Flashlight should check to see if the "other" is a "PickUp" like a battery... make sense?
Answer by Olgo · Dec 18, 2013 at 07:10 PM
This should be in a file named Flashlight.cs, see my comments for corrections I've made.
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;
//Declare lite outside of a function so all functions within the class have acess to it.
public Light lite;
private bool on;
private float timer;
void Start(){
//moved this to Start function. Start is called only one time, on start up during run time. thats all you need
lite = GetComponent<Light>();
}
void Update() {
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);
}
}
}
Now I will do the Battery Class...
This should be in a file called Battery.cs, attach the script to your battery component in game. this script will automatically add a spherecollider and make that sphere collider a trigger.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(SphereCollider))]
public class Battery : $$anonymous$$onoBehaviour {
void Start(){
//get the Sphere Collider component of this Battery and make it a trigger.
this.GetComponent<SphereCollider>().isTrigger = true;
}
//This function will run whenever another object with a collider encounters this object's collider.
void OnTriggerEnter(Collider other){
//Check to see of the GameObject that collides with this Battery is tagged as Player
//For this to work, you will need to set your controllable GameObject to Player
if(other.gameObject.tag == "Player") {
//Check to see if the player has a Flashlight Component so we don't get any errors in the next step
if(other.GetComponent<Flashlight>() != null){
//if the player has a flashlight, we continue to up the battery percentage.
other.GetComponent<Flashlight>().batteryPercentage += 10;
}
}
}
}
Lastly, I recommend following some tutorials that you can learn how to program from. I started about a year and a half ago and consider myself pretty proficient in the basics.
I started with this: http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial
a hack and slash RPG tutorial from scratch. awesome info here.
i get two errors
Battery.cs(20,21): error CS0019: Operator !=' cannot be applied to operands of type
method group' and null' Battery.cs(23,20): error CS0119: Expression denotes a
method group', where a variable',
value' or `type' was expected
byt the way thank you for you help ive been at this for two days...
Woops! sorry. I'm at work so I can't compile and run it. $$anonymous$$ade a stupid mistake of forgetting parenthesis after the GetComponent
should work now
Thank you the script compiles fine now i cant see to get it to trigger to add battery percentage but im at a better point now lol thank you again
Your answer
Follow this Question
Related Questions
Flashlight pickup, battery etc 3 Answers
Recharging battery script, help 1 Answer
Battery Pickup Regeneration 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers