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 Negidnyk · Feb 21 at 05:01 PM · if-statement

What is better : every frame reassigne value or use if statement?

I appropriate text value of UI element Text in Update , but can apply if-statement to not to do it every frame . What is better solution?

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 Captain_Pineapple · Feb 21 at 05:17 PM

I'd choose option C:

Don't do stuff like this in Update at all. Both ways clutter your code and take up performance. Something like setting some text on the UI should be done when it needs to be done.


Example: You want to update the amount of lives you have and have a text ui element that says "3 Lives".

From what i understand what you currently do is every update write healthLabel.text = currentlives.ToString() +" Lives".

Instead you should have a function which does this and is called only when your lives actually get changed. This would be the proper way to do it.

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 Negidnyk · Feb 21 at 05:38 PM 0
Share

@Captain_Pineapple Yes , but I have something like this :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class InteractionController : MonoBehaviour
 {
     RaycastHit hit;
     [SerializeField] LayerMask interactableLayer;
     [SerializeField] Text hintText;
     public static string SelectedObject;
     // Update is called once per frame
     void Update()
     {
         if(Physics.Raycast(transform.position, transform.forward, out hit, 5, interactableLayer))
         {
             hintText.text = hit.transform.gameObject.name;
             IInteractable interactable =  hit.transform.gameObject.GetComponent<IInteractable>();
             if(Input.GetKeyDown(KeyCode.F))
             {
                 interactable.Interact();
             }
         }
         else
         {
             hintText.text = "";
         }
     }
 }


When I interact with object , I want to display it's name (actually not only name , but it doesn't matter). And if I don't interact with any object , I set text empty(or disable Text object). So what shoud I do in such situation?

avatar image Captain_Pineapple Negidnyk · Feb 22 at 09:14 AM 0
Share

aah, fair point :)


In this situation, i'd just leave it like this. You should optimize single instances of something like this if you specifically know that they slow down your game significantly. Otherwise these kind of micro-optimizations just take up the time you could spend better elsewhere.


If you specifically want a really really small improvement then introduce a flag isTextEmpty that you check before setting the name empty. I'd not do something like this in your interactable name setting part as you'd have to check if the object has changed which takes up the same amount of performance needed to simply set the text.


As a sidenode: Later on if you do string concatenations here you might wanna revisit this with the profiler on. String concat always creates memory allocations which might be bad if done every frame.

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

133 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 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 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 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 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 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

Using multiples of 360 in if statements. 2 Answers

IF doesn't work 3 Answers

C# if-statement optimization 1 Answer

Checking a GUI.Textfield (C#) 1 Answer

If Application.LoadLevel fails, load mainMenu 0 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