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 awesomeaustin316 · Nov 04, 2014 at 10:00 PM · flashlightbattery

Flashlight Battery Error No definition

Sorry to come back so soon, but I tried coding a simple battery script to my flash light and I'm sorta stuck. I usually code java but this amazing flashlight script is in C#, so that's what I've been using. I'm stuck as to how I can fix this error with my two scripts and any help in the right direction is appreciated. Sorry I'm really new to C# so thank you all !

Error Assets/Scripts/Flashlight/Battery.cs(11,28): error CS0117: Flashlight' does not contain a definition for batteryPercentage'

Flashlight.cs

 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);
         }
     }
 }


Battery.cs

 using UnityEngine;
 using System.Collections;
 
 public class Battery : MonoBehaviour {
     //Put this script on the pickup
     
     public int batteryPower = 10; //The amount of battery power to give, negative value hogs energy instead
     
     void  OnTriggerEnter ( Collider other  ){
         if (!other.CompareTag("Player")) return;
         Flashlight.batteryPercentage(batteryPower);
         Destroy (gameObject);
     }
 }
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 awesomeaustin316 · Nov 04, 2014 at 10:29 PM 0
Share

This got pushed to the back of the line fast.

avatar image Serinx · Nov 04, 2014 at 10:40 PM 0
Share

Are you trying to set the batteryPercentage to batteryPower?

It's looking for a function called batteryPercentage which doesn't exist in your Flashlight class.

If you want to set the public variable you just need something like:

 Flashlight.batteryPercentage = batteryPower;
avatar image AlwaysSunny · Nov 04, 2014 at 10:45 PM 0
Share

Also, you appear to be trying to access an instance member with the class itself. Unless your variable is static, this isn't what you need. You need to find the Flashlight component on the object.

Flashlight someFlashlight = other.GetComponenet() as Flashlight;
someFlashlight.batterPercentage = whatever;

avatar image awesomeaustin316 · Nov 04, 2014 at 10:57 PM 0
Share

Thank you guys, like I said Im fairl new

avatar image awesomeaustin316 · Nov 04, 2014 at 11:06 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class Battery : $$anonymous$$onoBehaviour {
     //Put this script on the pickup
     
     public int batteryPower = 10; //The amount of battery power to give, negative value hogs energy ins$$anonymous$$d
     
     void  OnTriggerEnter ( Collider other  ){
         if (!other.CompareTag("Player")) return;
         Flashlight someFlashlight = other.GetComponenet() as Flashlight;
         someFlashlight.batteryPercentage = batteryPower;
         Destroy (gameObject);
     }
 }




now gives me error

 Type `UnityEngine.Collider' does not contain a definition for `GetComponenet' and no extension method `GetComponenet' of type `UnityEngine.Collider' could be found (are you missing a using directive or an assembly reference?)
Show more comments

1 Reply

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

Answer by Ricewind1 · Nov 05, 2014 at 01:00 AM

You need to reference the script you are using first, like below:

 Flashlight fLight = other.GetComponent<Flashlight>(); //Assuming that "other" has the flashlight as a script on its gameobject
 fLight.batteryPercentage += batteryPower;
 
Comment
Add comment · Show 8 · 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 awesomeaustin316 · Nov 05, 2014 at 01:21 AM 0
Share

$$anonymous$$y new battery code is

 using UnityEngine;
 using System.Collections;
 
 public class Battery : $$anonymous$$onoBehaviour {
     //Put this script on the pickup
     
     public int batteryPower = 10; //The amount of battery power to give, negative value hogs energy ins$$anonymous$$d
     void  OnTriggerEnter ( Collider Player){
         Flashlight fLight = Player.GetComponent<Flashlight>(); //Assu$$anonymous$$g that "other" has the flashlight as a script on its gameobject
         fLight.batteryPercentage += batteryPower;
         Destroy (gameObject);
     }
 }


It doesn't give any errors until I put it on a cube , untaged, with a box collider trigger, and when I walk into it, it gives me an error at line 10

avatar image awesomeaustin316 · Nov 05, 2014 at 01:21 AM 0
Share

NullReferenceException: Object reference not set to an instance of an object Battery.OnTriggerEnter (UnityEngine.Collider Player) (at Assets/Scripts/Flashlight/Battery.cs:10)

avatar image Ricewind1 · Nov 05, 2014 at 01:26 AM 0
Share

Check if it's really the player. Also, is the flashlight a script ON the player?

 void  OnTriggerEnter ( Collider col){
 if(col.tag == "Player")
 {
          Flashlight fLight = col.GetComponent<Flashlight>(); //Assu$$anonymous$$g that "other" has the flashlight as a script on its gameobject
          fLight.batteryPercentage += batteryPower;
          Destroy (gameObject);
      }
 }
avatar image awesomeaustin316 · Nov 05, 2014 at 02:05 AM 0
Share

I'm testing it now, but I'm using UFPS and it's set up where the flashlight script is on my spotlight itself, which is tagged Player

avatar image awesomeaustin316 · Nov 05, 2014 at 02:10 AM 0
Share

NullReferenceException: Object reference not set to an instance of an object Battery.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Scripts/Flashlight/Battery.cs:12)

The error I get using your code. The set up is the Spotlight is labeled Player, has the flashlight script, and I put the Battery script on a cube with no tag and a trigger

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

30 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Recharging battery script, help 1 Answer

Flashlight battery script 3 Answers

Flashlight pickup, battery etc 3 Answers

Pressing "Fire2" Spawns and removes object on click 0 Answers

How do I make a power out on my flashlight? 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