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 Entyro · May 24, 2013 at 03:22 PM · flashlightendgamebattery

Battery life after the game ends PROBLEM

Hi! I have a problem with my battery power, for the flashlight, after the game ends. At first when you start the game it's fine, but when you finished the game and comes to the main menu and then restart it, the battery power is the same as it was when the game ended. It only works if you reload the game but that's not good. Please help!

alt text

Flashlight script:

 var BatteryPower : Texture;
 var lightInner : Light; //Connect the light source in the Inspector
 var lightOuter : Light; //Connect the light source in the Inspector
 var FlashLightSound : AudioClip;
 static var energy : float = 100; //The energy amount of the flashlight
 static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
 private var drainSpeed : float = 0.3; //The speed that the energy is drained
 
 function Update () {
     if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();
     
     
    
     if (Input.GetKeyDown(KeyCode.F)) {
 
     audio.PlayOneShot(FlashLightSound);
     
     }
     
 }
 
 
 
 //When the player press F we toggle the flashlight on and off
 function ToggleFlashlight () {
     turnedOn=!turnedOn;
     if (turnedOn && energy>0) {
        TurnOnAndDrainEnergy();
     } else {
        lightInner.enabled = false;
        lightOuter.enabled = false;
     }
     
 }
 
 
 function OnGUI () {
     GUI.BeginGroup( Rect(40, 10, 218 * (Flashlight.energy/100.0) +35 , 90) );
     GUI.Label( Rect(0, 0, 256, 128), BatteryPower );
     GUI.EndGroup();
 }
 
 //When the flashlight is turned on we enter a while loop which drains the energy
 function TurnOnAndDrainEnergy () {
     lightInner.enabled = true;
     lightOuter.enabled = true;
     
     while (turnedOn && energy>0) {
        energy -= drainSpeed*Time.deltaTime;
        yield;
     }
     lightInner.enabled = false;
     lightOuter.enabled = false;
 }
 
 //This is called from outside the script to alter the amount of energy
 static function AlterEnergy (amount : int) {
     energy = Mathf.Clamp(energy+amount, 0, 100);
 }


battery.png (447.4 kB)
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 ExTheSea · May 24, 2013 at 03:41 PM 0
Share

I would say you just have to reset all the related variables when you start a new game.

Btw. Is this the rights script because it says something about safe and nothing about a flashlight or batteries.

avatar image Entyro · May 24, 2013 at 03:47 PM 0
Share

It wasn't the right script, thanks :)

avatar image ExTheSea · May 24, 2013 at 03:50 PM 0
Share

So i think it's what i though it will be. You have to call a function when you start a game which resets the energy to 100. $$anonymous$$aybe you have to reset some other variables too.

avatar image Entyro · May 24, 2013 at 04:04 PM 0
Share

Ok, do you have any idea how I can do that. I'm not that great with coding :)

avatar image ExTheSea · May 24, 2013 at 04:08 PM 0
Share

Well there is this function which gets called when the level is loaded: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.OnLevelWasLoaded.html

Although normally when a level is loaded all variables are reset but you can try it out.

Show more comments

1 Reply

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

Answer by clunk47 · May 24, 2013 at 03:46 PM

Sounds like you're staying in the same level in your build, possibly? If you're not wanting to load a new level, try resetting any variables that need to be reset at the "level end" function, whatever that may be. Otherwise, if you load a new level, or reload the current level, your scripts will be reset. Check out Application.LoadLevel

EDIT:

Don't use static variables. If you want something to keep its data, use DontDestroyOnLoad instead.

To access something, just have an empty game object in your scene with the scripts attached that you want to access. EXAMPLE:

Say you have a gameobject named (scripts), and a script attached named (testscript). In that script, you have a variable (var testVar : int = 0;). You want to change that.

var scriptObject : GameObject; (drag your scripts object into the inspector)

scriptObject.GetComponent(testscript).testVar = 100;

Comment
Add comment · Show 9 · 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 clunk47 · May 24, 2013 at 04:37 PM 1
Share

Saw you were using static variables when you added the correct script, edited my above answer. Check out DontDestroyOnLoad. static is only used if there is only one instance of something, ever.

avatar image clunk47 · May 24, 2013 at 04:39 PM 1
Share

when you access something static, it's (scriptname.variable). Ins$$anonymous$$d, when not using static, use GetComponent. I'll edit my answer again.

avatar image clunk47 · May 24, 2013 at 04:44 PM 1
Share

Answer edited, see if the additional info helps.

avatar image Entyro · May 24, 2013 at 04:56 PM 1
Share

Sorry for maybe stupid questions but what am I gonna use this script for? var testVar : int = 0; var scriptObject : GameObject; scriptObject.GetComponent(StaticReset).testVar = 100;

Isn't it any simpler way to do it? As I said to "ExTheSea", when I'm removing static I get errors in the script.

avatar image ExTheSea · May 24, 2013 at 05:10 PM 1
Share

You can of course do it using static variables but you have to be aware that you might get into trouble later since they can only exist one instance per class/script. This might work now but it's possible that you're digging a hole you later can't get out easily.

It's always best to avoid using static variables if possible.

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

15 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

Related Questions

Multiple Cars not working 1 Answer

Need help with a script to Pick up Batteries for a Flashlight type Lighter 1 Answer

Gui placement help 1 Answer

Main Menu: Is it better to subdivide it into different scenes? 1 Answer

Script is not working at all 2 Answers


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