Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by VS48 · Oct 22, 2010 at 07:05 PM · performancegetcomponentbestpractices

Accessing other objects efficiently

When an object touches the trigger, the trigger wants to know how to deal with this object. Calling GetComponent() several times per frame is not very efficient, I've read. Tags cannot be used because they're not strictly associated with components.

Is there an efficient way to access component code of an object at runtime (such as from OnTriggerStay). For example, some game engines would have something like a collider.gameObject.userData object which can be cast at runtime yielding decent performance (although that's somewhat nasty as well). The userData would be a class that caches all the components at load time, for example.

Thanks!

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 lampshade · Oct 22, 2010 at 07:22 PM 0
Share

Attaching a script to the game object with a type of collider that your player or some body is colliding with, marking it as a trigger, and acting upon that event in OnTriggerEnter, Stay or Exit doesn't work out?

avatar image VS48 · Oct 22, 2010 at 08:29 PM 1
Share

How would OnTriggerStay know what type of object touched the trigger? Without calling GetComponent (potentially) many times per frame?

1 Reply

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

Answer by skovacs1 · Oct 22, 2010 at 08:28 PM

You're asking how to avoid GetComponent(), right?

GetComponent returns the component instance of something somewhere. To avoid calling GetComponent you must to get the component and store it somewhere more accessible.

Storing the component in general

Since you don't really know what you're colliding with, simply storing the collided object may not work for you. If you only have certain things that can collide with this, then you can store and check against those. If your use case promotes recurrent collisions, you could still cache the last object & component that were collided with and when a new collision occurs, check and use them if they're the same, otherwise chuck them and GetComponent.

Static = everybody knows about it

Using statically scoped functions and variables would make things even easier, but such scripts may need to be a little smarter than the average script because static functions cannot access the public Monobehaviour-local variables that would refer to the instance to which the script is attached. If there's only a few objects, this is viable and potentially more efficient solution as a static script with a static, quicker-to-search container (HashSet for example) that contains references to the things you care about colliding with, can be quickly searched, which is likely less overhead than getComponent;

Every Frame?

You say you're concerned about calling GetComponent every frame. Why would you do that then? You could avoid OnTriggerStay by storing the component of the object OnTriggerEnter and removing the it OnTriggerExit, performing whatever it is you were doing OnTriggerStay within Update to each component stored and this would call GetComponent only OnCollisionEnter and maybe OnCollisionExit, but not every frame.

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 VS48 · Oct 25, 2010 at 02:53 PM 1
Share

Another thing that worked in my case (I'm ashamed to admit this just came to me) is changing the trigger's receiver. 90% of the collisions that matter to me occur between the character and the game object -- so implementing the trigger callback in the objects, and comparing the character's static instance with the collider GameObject is a way to get rid of 90% of GetComponent calls in my case.

Thanks for the answer!

avatar image VS48 · Oct 25, 2010 at 02:55 PM 0
Share

I was also able to reduce GetComponent calls further by adding a component that caches all components present at load time. Then, all subsequent component lookups are made through that component to guarantee only 1 lookup per object. Again, probably pretty obvious and doesn't always help, but worked in my case and just wanted to share! Thanks again

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

No one has followed this question yet.

Related Questions

SendMessage or GetComponent 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I select a joint component from a GameObject that has multiple joint components? 1 Answer

accessing script components from external objects 2 Answers

Implementing Setter/Getter 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