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 $$anonymous$$ · Aug 11, 2013 at 06:11 PM · guitextureifenableddisabled

Need help with Enabling/Disabling GUITextures with script

My script is for a flash light, the idea is that when the variable energy goes below 50 the GUI of a green battery disappears. The issue I am having is that my script doesn't work with the drainspeed variable, the GUI doesn't disappear when it's drained below 50, however i wonder if this is an issue with the function only being read once...

 var lightSource : Light; //Connect the light source in the Inspector
 var bat1 : GUITexture;
 static var energy : float = 60; //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 = 10.0; //The speed that the energy is drained
  
 function Update () {
     if (Input.GetKeyDown(KeyCode.E)) ToggleFlashlight();
 }
  
 //When the player press F we toggle the flashlight on and off
 function ToggleFlashlight () {
     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 Start(){
 
 if(energy > 50){
 bat1.enabled = true;
 }
 if(energy <50){
 bat1.enabled = false;
 }}

and this is my script

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 Joyrider · Aug 11, 2013 at 06:15 PM

You should call enabled and set it to false. Instead of calling disabled.

The disabled property doesn't exist on any type of object.

Comment
Add comment · Show 4 · 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 $$anonymous$$ · Aug 11, 2013 at 06:20 PM 0
Share

Thanks that removed the error but, I still don't know how to get the image to disappear when it's "drained" below 50, any ideas?

avatar image Joyrider · Aug 11, 2013 at 06:28 PM 0
Share

You should put the code you have in Start() (which is only executed once, at the very beginning) in your update function; that shoud do the trick.

avatar image wijesijp · Aug 11, 2013 at 06:32 PM 0
Share

do the same check you do at start, in function "TurnOnAndDrainEnergy "

avatar image $$anonymous$$ · Aug 11, 2013 at 06:33 PM 0
Share

Oh damn! Noob error! The amount of stupid I feel now passing through my body >.< !!! =D thanks a bunch=3

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

Show GUI texture then not 0 Answers

A Button, A Boolean and 2 Textures. 2 Answers

Wearied Error 1 Answer

gameObject.enabled is not working 1 Answer

Interactive pie-shaped GUI 3 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