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 /
This question was closed May 15 at 09:02 PM by Nakoru.
avatar image
0
Question by Nakoru · Sep 14, 2021 at 09:16 PM · performance optimizationreferencingoptimisationfinding

Very basic optimisation question (Find)

Hi. I have a player with its references to its own components like collider/rb in a player.cs script. The thing is, referecing manually in the engine makes it look ugly, and I was wondering : Is referencing like rb = this.GetComponent<Rigidbody2D>(); is okay ? It may use a lot of ressources as a level of my game is composed of multiple scenes and there's no DoNotDestroyOnLoad object, so it searches anew in each scene.

It helps to keep cleaness in my inspector ^^', tough I can help a little with Headers.

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

  • Sort: 
avatar image
0

Answer by julzerinos · Sep 14, 2021 at 09:44 PM

That's is actually the standard approach for objects not initialized in the inspector (dynamically, or in code-focused projects), but a key consideration is caching. If you're calling GetComponent every frame, that may lead to performance bottlenecks, especially dangerous as the number of attached components increases. You will be fine with caching the references and then just calling them as you need them.


See an example of how I set up my MonoBehaviors with Rigidbodies below. You can of course do this with any components.


 [RequireComponent(typeof(Rigidbody))]
 public class PlayerMove : MonoBehaviour
 {
     private Rigidbody _rg;
 
     private void Awake()
     {
         _rg = GetComponent<Rigidbody>();
     }
 
     private void FixedUpdate()
     {
         _rg.AddForce(Vector3.forward, ForceMode.Force);
     }
 }

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 Nakoru · Sep 15, 2021 at 09:55 PM 0
Share

Yep, I'm calling once in the Start() function, but I was considering if it's heavy because I'm going a speedy-game where you basically "swipe areas as fast as possible", and a level is made of multiple scenes. Sorry for not giving that detail in the question, I understand this mistake and won't do again.

My game is very similar to Hotline Miami if you want a visual example :

https://youtu.be/XOAY5p3d02Q

avatar image julzerinos Nakoru · Sep 16, 2021 at 07:17 AM 0
Share

@Nakoru

No worries, but I still would say that this is a negligible cost. Of course it depends how often you load a new scene. Using the inspector to set the values might be a tiny fraction faster, as Unity is just injecting the value before Awake or Start, whereas in those event functions you have to spend a bit of processing power to find the components. IIRC GetComponents logic is to loop through all the components with a stop condition on the component type, so depends on the component count.

One other consideration - I assume this is the player rigidbody we are talking about right? The way I would approach it is to keep the player alive across all scenes. You can use DontDestroyOnLoad for that. Then the player script remains the same instance and your rigidbody reference should remain the same.

One remark to the remark - you can also load scenes additively (on top of existing scenes). It's also worth taking into account that transferring the logic for adding new objects instead of scenes may keep things in check as well.

Hope this helps. In the end the best answer to optimization is one you have to answer yourself - open up the Profiler (it doesn't bite) - plop in some Profiler.BeginSample and observe the differences.

Follow this Question

Answers Answers and Comments

127 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

Related Questions

Are motion captured animations compatible on mobile? 0 Answers

Optimising references to other scripts 1 Answer

Text Update demands too much performance 0 Answers

Problem of creating smooth animations 3 Answers

How to debug or otherwise troubleshoot a specific graphics frame (not current)? 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