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 EagaBeava · May 13, 2013 at 11:19 PM · performancegetcomponentreferencestaticaccess

Methods of Accessing scripts - performance & neatness

I've been using Unity and C# for a few months now and have always wondered about the various ways to access scripts on another object. For instance, I usually cache the references (using GetComponent) in Awake/Start and this does work well enough but can sometimes bloat the functions if there are many scripts that I need access to.

Another method that Ive recently starting using is to create a public static Scriptname reference; in the script I want to access, which then references the script using Scriptname.reference = this; and then used in other scripts by calling Scriptname.reference.variable/function; to access what I want. I've found this to be generally easier to read that the other method

I was wondering if anyone can give me an overview of when to use each method (or any others that I don't know about), both from a performance as well as a neatness/elegance point of view.

Thanks in advance.

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 Lairinus · May 14, 2013 at 04:11 AM

I'll tell you what I know --

Generally, static instance variables are used when you need to access ONE specific GameObject such as a camera or something that there's only ever one of. It makes it easier than getcomponent because you could do something like "PlayerCam.Instance.Hero = null."

Caching GetComponents is a very good way for scripts to talk to each other. The performance expense comes in when you call it every frame (doesn't sound like that's your issue)

-As far as I'm aware- Caching GetComponents is approximately 100x faster than SendMessage (can't quote the post, found it a few months ago).

Basically, for performance you want to Cache GetComponents and use SendMessage as sparingly as possible. As with damage functions, using either an instanced GetComponent or SendMessage, I -believe- SendMessage would be superior simply because you need to get the individual unit per damage interval.

What I personally did for (at least temporarily) my scripts was parent them. I have approximately 30 "state" scripts including health movement speed object state orientation etc, and I broke them down into "StatManagers," "ActionManagers," "Animations," and "Skills." The parent script has each object GetComponent-ed and I can reference each instance of each variable with only the basic 30 GameObjects "parented" to each other. If any script needs any value, I can easily do parentObject.GetComponent(Parent).Stats.Health.Current, for any "state" script.

Just my 2c, I know that at least a majority of it is true including the caching of GetComponents and SendMessage's performance. Optimization is a thing that takes the back burner until you know that something has to change because people tend to over-think organization and end up scratching their heads at their own code.

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

15 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

Related Questions

how do you access booleans from another script 1 Answer

Referencing booleans from settings 1 Answer

How to access the interface from the last component in the inspector 2 Answers

once again, Sendmessage VS Getcomponent! 1 Answer

Changing variable of another object (C#) 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