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
-2
Question by StickyDream · Aug 18, 2013 at 03:46 PM · errorcolliderpickupno

Cant turn on flashlight

When i Picked Up the FlashLight I cant Activate it but unity gives me no error I have three scripts HUD FlashLight And Pickup Here They Are can Anyone Please Help me

FlashLight.cs

 public class Flashlight : MonoBehaviour 
 {
         public static float BatteryLife = 100;
         public static GameObject FlashLightMount;
         
         public Light HeadLight;
         public float BatteryReductionSpeed = 3.0f;
         
         void Awake()
         {
             FlashLightMount = GameObject.FindWithTag("FlashLight");
             HeadLight.enabled = false;
         }
         
         void Update()
         {
             if(HeadLight.enabled)
             {
                 BatteryLife = BatteryLife - (BatteryReductionSpeed * Time.deltaTime);
             }
             
             if(Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && HeadLight.enabled)
             {
                 if(BatteryLife <= 0 && HUD.BatteryCount > 0)
                 {
                     HUD.BatteryCount--;
                     BatteryLife = 100;
                 }
                 
                 HeadLight.enabled = true;
             }
             else if(Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && HeadLight.enabled)
             {
                 HeadLight.enabled = false;
             }
             
             if(BatteryLife <= 0)
             {
                 BatteryLife = 0;
                 HeadLight.enabled = false;
             }    
         }
 }

HUD.cs

 using UnityEngine;
 using System.Collections;
 
 public class HUD : MonoBehaviour 
 {
     public static int BatteryCount;
     public static bool HasFlashlight;
         
     void OnGUI()
     {
         if(HasFlashlight)
         {
             GUI.Label(new Rect(10,10,80,30), "Flashlight" );
             //GUI.Label(new Rect(70,10,80,30), Flashlight.BatteryLife.ToString("F2"));        
         }
         
         GUI.Label(new Rect(10,40,80,30), "Batteries" );
         GUI.Label(new Rect(70,40,80,30), BatteryCount.ToString());
         
     }    
 }
 

Pickup.cs

     using UnityEngine;
     using System.Collections;
      
     public class Pickup : MonoBehaviour {
     public enum Item { Flashlight, Battery }
     
     
      
     public Item item;
     public AudioClip flashlightSound;
     public AudioClip batterySound;
      
     void OnTriggerEnter()
     {
     if(item == Item.Flashlight)
     {
     Flashlight.FlashLightMount.SetActiveRecursively(true);    
     Flashlight.FlashLightMount.active = true;
     AudioSource.PlayClipAtPoint(flashlightSound, transform.position, 1);
     HUD.HasFlashlight = true;
     }
     else
     HUD.BatteryCount++;
     AudioSource.PlayClipAtPoint(batterySound, transform.position, 1);
      
     Destroy(gameObject);
     }
 }
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
0

Answer by ShadoX · Aug 18, 2013 at 06:56 PM

Are you sure that "HeadLight.enabled" is actually set to true? Because the code wont work if it isn't.

  if(Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && HeadLight.enabled)
  {
   if(BatteryLife <= 0 && HUD.BatteryCount > 0)
   {
       HUD.BatteryCount--;
       BatteryLife = 100;
   }
 
   HeadLight.enabled = true;
  }
  else if(Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && HeadLight.enabled)
  {
   HeadLight.enabled = false;
  }

Both check if the HeadLight is enabled. So I guess that you wanted to check in one of them if it isn't enabled and if it isn't you do that in the first 'if'.

I guess that you could try adding a ! in the first if ...

 if(Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && !HeadLight.enabled)
Comment
Add comment · 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

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

16 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

Related Questions

Android: Application.LoadLevel("level_name") does not do anything 1 Answer

Counting colliders 2 Answers

Error with trigger PLZ HELP!!!! 1 Answer

(Solved) Collision happens, but is not detected? 1 Answer

Javascript error.. insert semicolon?? 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