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 ImPvEspec · May 16, 2013 at 06:01 PM · booleantime.deltatimesendmessage

How can i keep making the SendMessage repeat as long as the boolean is true

I have a kind of energy bar where when i collect enough i can instantiate an object, but i want the the energy to drain over time for as long as that object is alive.

I can make it reduce it once but struggling to make it repeat, pretty sure i'd be doing something in the if(orbAlive == true){ guiRef.SendMessage("SetFocus", spentAmount)); line i've tried a few things and still no success, so my question is how can i make the value of spentAmount be subtracted every second

 var spentAmount : float = 1;
 var particle:GameObject;
 private var guiRef:GameObject;
 private var orbAlive: boolean = false;
 
 
 function FireAndReload (){
         
     if(GameObject.Find("__GameMaster").GetComponent(Score).focus >= 5){
         
     //Instantiate stuff in in here then setting the boolean true
                 orbAlive = true;
     }
                 if(orbAlive == true){
                 guiRef.SendMessage("SetFocus", spentAmount));
                              }
                     if(GameObject.Find("__GameMaster").GetComponent(Score).focus <= 0){
                         orbAlive = false;
                         Destroy(particle);
                     }
     }
 }    
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 ExTheSea · May 16, 2013 at 06:31 PM

The direct answer would be to do this:

 function FireAndReload(){
 ....
 if(orbAlive == true){
  InvokeRepeating("EverySecond", 1.0, 1.0); //Calls EverySecond every second
 }
 ...
 }

 function Update(){
 {
  if(orbAlive == false)
  CancelInvoke("EverySecond"); //Stops EverySecond
 }
 ...
 }


 function EverySecond(){
 guiRef.SendMessage("SetFocus", spentAmount));
 }

but i would recommend you to maybe let the script you send the message to handle the EverySecond part. Using SendMessage every second is not very performance-friendly.

Warning: This code is untested.

Hope it works for you.

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 ImPvEspec · May 17, 2013 at 09:59 AM 0
Share

bah i can make it deduct once it becomes active but i cant stop it lol

avatar image ExTheSea · May 17, 2013 at 04:02 PM 0
Share

So your problem now is that EverySecond isn't stopped getting called? Do you ever set orbAlive to false?

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

14 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

Related Questions

Check how long a bool has been true 2 Answers

Returning bool from another script (Cannot implicitly convert type 'void' to 'bool') 2 Answers

Delay Player From Using Action Button For A Period Of Time 0 Answers

Log a quickly changing value without boolean flags 0 Answers

SendMessage and boolean... my GO doesn't keep the value. 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