Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 laradov · Jul 25, 2015 at 08:24 AM · updatescriptingbasicsif-statementsefficiencyinterrupt

Do something when health is below X

Hi, I want to do something when the health of the character is below X. I know the easiest and the common way is

 void Update(){
    if(health<X) DoSomething();
 }

But it is highly inefficient since it will check the health in every frame. If game lasts hours, imagine the wasted processing power. If I need to check many things, there will be many if statements in the Update and code will be messy too. Also if DoSomething() will be used for once (like give health bonus for once) in the game, there will be an useless always-false if statement in every frame during the game after that. Are there any other ways (like interrupts) to do this?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Hexer · Jul 25, 2015 at 08:59 AM

1 line of code with an if-statement will not highly affect your preformance on the long run. The line of codes that do affect your preformance if used too much are.

*GameObject.Find

*GameObject.FindWithTag (not as expensive as .Find but with alot of tags still troublesome)

*Resources.Load (never use this, it is uber slow. surely for mobile devices)

*Debug.Log (Get rid of this line of code when you are done making your game)

Next to code, the most important is to get the polycount(verts and tris) of your scene as low as possible. And don't make too much use of the light tools in Unity.

That all said, your method is perfectly fine.

EDIT : You can also use InvokeRepeating

 void Start(){
 InvokeRepeating("Brackets",0.1f,1.0f);
 }
 
 void Brackets(){
 if(health >= 0){
 Application.LoadLevel(0);
 }
 }


This is like the Update function where you can change the parameters to your liking.

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

Answer by ian_sorbello · Jul 25, 2015 at 09:02 AM

You need to use CoRoutines. http://docs.unity3d.com/Manual/Coroutines.html

So instead of the update function, do your checking in a routine that executes much less frequently.

This could also sort out the need for the "only once" scenario:

 using System;
 using System.Collections;
 using UnityEngine;
 
 public class BonusCheck : MonoBehaviour {
 
     public float delay = 0.1f; // Run every 100ms.
     private int points = 0;
 
     // Use this for initialization
     void Start () {
         StartCoroutine (CheckForBonus ());
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     IEnumerator CheckForBonus() {
 
         bool bonusAwarded = false;
 
         while(!bonusAwarded) {
 
             // Points greater than 200?
             if(points > 200) {
                 AwardBonus();
                 bonusAwarded = true;
             }
 
             yield return new WaitForSeconds(delay);
         }
     }
 
     private void AwardBonus() {
         // Do award
     }
 
 }
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

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

Related Questions

Efficient way to assign a value from another value 1 Answer

Efficiency of Game Loops 2 Answers

how to start coroutines in the update method? 3 Answers

How to call unity event once every time my object reach destination in Update()? 1 Answer

Why is OnGUI not working? 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