Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Deatructo · Dec 18, 2013 at 02:36 PM · pickupflashlightbattery

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; 
               }
     }
Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sdgd · Dec 18, 2013 at 02:37 PM 0
Share

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.

avatar image fafase · Dec 18, 2013 at 02:41 PM 2
Share

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.

avatar image Deatructo · Dec 18, 2013 at 03:26 PM 0
Share

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?

avatar image Deatructo · Dec 18, 2013 at 05:30 PM 0
Share

It doesn't work i get the same error i always get $$anonymous$$ey word void can not be used in this context......

avatar image Olgo · Dec 18, 2013 at 06:03 PM 0
Share

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?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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...

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Olgo · Dec 18, 2013 at 07:21 PM 0
Share

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;
             }
         }     
     }
 }
avatar image Olgo · Dec 18, 2013 at 07:31 PM 0
Share

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.

avatar image Deatructo · Dec 18, 2013 at 07:32 PM 0
Share

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...

avatar image Olgo · Dec 18, 2013 at 07:35 PM 0
Share

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

avatar image Deatructo · Dec 18, 2013 at 07:56 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

When "Player" enters a trigger = flashlight off 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges