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 Geta-Ve · Mar 28, 2014 at 10:38 AM · updatefunctionmemoryonce

Run function once, is this method appropriate?

So, I am running an if statement through my Update function checking whether or not the player is dead, IF he is then run the PlayerDead function (show score, etc).

Obviously I only need to run it once and not every frame, so I wrote this piece of code -- which works -- though I am uncertain of whether or not this is appropriate / advisable.

I am quite new to programming and really have no clue about memory management, but I assume checking for an if statement every frame would be no different from Update checking if ANYTHING at all is happening every frame.

Thanks for any input you guys can afford. :)

 #pragma strict
 
 private var runOnce : boolean = true;
 
 function PlayerDead()
 {
     player death stuff here
 }
 
 function Update()
 {
     if(health == 0 && runOnce == true)
     {
         PlayerDead();
         runOnce = false;
     }
 }
Comment
Add comment · Show 2
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 Geta-Ve · Mar 28, 2014 at 10:46 AM 0
Share

Thanks Gruffy! I am definitely going to make that change. Though I do have the health clamped between 0 and 999 I think you are right that I might as well check if for any reason at all the health goes lower than zero.

$$anonymous$$uch appreciated.

Not sure if I can change this question to answered, but, you answered, so cheers on you. ^_^ Thanks!!

avatar image Gruffy · Mar 28, 2014 at 10:48 AM 0
Share

Just changed it to answer for you bud, thanks much appreciated. Gruffy

2 Replies

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

Answer by Gruffy · Mar 28, 2014 at 10:43 AM

Hello bud, the above is absolutely fine.

If you really wanted you could change the "==" to "Comment

Add comment · Show 1 · 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 Geta-Ve · Mar 28, 2014 at 10:50 AM 0
Share

Thanks again. :)

avatar image
0

Answer by onecolor · Mar 28, 2014 at 11:03 AM

i guess your player is restoring its health after death.

if you restore your player's health in the PlayerDead() function you can do the check without the flag.

 function PlayerDead()
 {
     player death stuff here
     health = 999;
 }
  
 function Update()
 {
     if(health <= 0)
     {
        PlayerDead();
        
     }
 }
Comment
Add comment · Show 3 · 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 Geta-Ve · Mar 28, 2014 at 11:07 AM 0
Share

Interesting idea! Though, right now I have my health stored in a separate script with a set and get function. I am doing it this way simply because I needed to access the health from 3 different locations.

health display health decrement health increment

and set/get seemed like the best way to do it. (again I am new to program$$anonymous$$g, so there could very well be a better / easier way to do it -- though, to be truthful, I WAS using PlayerPrefs to store my health information >_> )

avatar image onecolor · Mar 28, 2014 at 11:14 AM 0
Share

if you restore your health (in PlayerDead() or wherever) i think that the simplified if statement (without the flag) could works fine.

avatar image Geta-Ve · Mar 28, 2014 at 11:24 AM 0
Share

Thanks dude! Though, I guess I should have mentioned that I am not actually restoring health immediately after the player dies. The current game "session" ends, and the highscore and what have you is displayed.

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Call A Function At A Certain Time 1 Answer

Calling a function only once in Update 6 Answers

Calling a method once in update 1 Answer

audio.PlayOneShot() + Update() = mess? 1 Answer

Does using an empty or inactive Update function take up memory? 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