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 Kobohdashi · Aug 08, 2013 at 08:24 PM · levelstartflashlightoff

How to set flashlight to off...

Hello Everyone my game is a cross between a horror survival game and a rpg/exploration game. The player is equipped with a flashlight from the very start of the game and has to find batteries to power the flashlight, using some tutorials and some reference scripts I have all that working the problem I have encountered is that the flashlight is on when the level starts. How would I go about setting the flashlight to be off when the level starts? Here is the script

 //Name this script Flashlight and attach it to your player for instance
 var switchsound : AudioClip;
 var lightSource : Light; //Connect the light source in the Inspector
 static var energy : float = 0; //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 = 2.0; //The speed that the energy is drained
 
 
 
 function Update () {
     if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();
 }
 
    
 //When the player press F we toggle the flashlight on and off
 function ToggleFlashlight () {
          audio.PlayOneShot(switchsound);
     turnedOn=!turnedOn;
     if (turnedOn && energy>0) {
        TurnOnAndDrainEnergy();
     } else {
        lightSource.enabled = false;
     }
 }
  
 //When the flashlight is turned on we enter a while loop which drains the energy
 function TurnOnAndDrainEnergy () {
     lightSource.enabled = true;
     while (turnedOn && energy>0) {
        energy -= drainSpeed*Time.deltaTime;
        yield;
     }
     lightSource.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);
 }
 
 function OnGUI () {
     GUI.Label (Rect (10, 10, 150, 20), Flashlight.energy.ToString());
 }
 

any guidance would be greatly appreciated. Muchas Gracias

Comment
Add comment
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

1 Reply

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

Answer by roojerry · Aug 08, 2013 at 08:30 PM

How bout just disabling the light on Start

 function Start(){
     lightSource.enabled = false;
 }


or un-ticking the toggle on the light component attached to the gameobject

Comment
Add comment · Show 2 · 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 Kobohdashi · Aug 08, 2013 at 08:38 PM 0
Share

@brianruggieri Thank you that worked I had tried that but it did not work before maybe I was not putting it in the right place but it works now muchas gracias.

avatar image Kobohdashi · Aug 23, 2013 at 01:21 AM 0
Share

@brianruggieri The problem I am having now is that it works when I'm working in the editor but when I build and run it the flashlight does not turn on at all.

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

Initialising List array for use in a custom Editor 1 Answer

How to turn off flashlight once in water? 1 Answer

How do I load the first level? 2 Answers

Scene starts before scene loading is finished 0 Answers

Start/Awake cut off at a certain point? 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