Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Ted-Brown · Aug 09, 2011 at 12:59 AM · getcomponent

Can you shorten "GameObject.GetComponent(Script)" to a variable?

When visually parsing my scripts, my eyes just get totally jangled by the constant, long references to GameObject.GetComponent(Script), such as:

 runnersObj.GetComponent(RunnerControlScript).GetBatterObj();

In my ideal world, I could write something like this:

 var RunnerControl : scriptReference; // fake type for illustration purposes only!
 RunnerControl = runnersObj.GetComponent(RunnerControlScript);
 // later in code...
 RunnerControl.GetBatterObj();

This would greatly beautify the code, IMHO.

Is this possible, and if possible, is it recommended?

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 Chris D · Aug 09, 2011 at 01:02 AM 0
Share

have you checked out this question?

http://answers.unity3d.com/questions/148949/assign-script-into-another-script-in-inspector.html

avatar image Eric5h5 · Aug 09, 2011 at 04:09 AM 0
Share

You can left type inference take care of it:

 var RunnerControl = runnersObj.GetComponent(RunnerControlScript);

Note: only works properly in Unity 3.4. In earlier versions GetComponent returns Component rather than the type of the script, so you have to either use generics or cast it manually. Another note: type inference has nothing to do with dynamic typing, and has no effect on speed; it's purely a compiler convenience.

3 Replies

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

Answer by Josh 14 · Aug 09, 2011 at 01:23 AM

Try something like this:

 var RunnerControl;
 var runnersObj:GameObject;
 
 function Start()
 {
     RunnerControl = runnersObj.GetComponent("NameOfScript");
 }
 
 function Update()
 {
     Debug.Log(RunnerControl.someVariable);
     RunnerControl.SomeFunction();
     // Other stuff to do with RunnerControl
 }

Just remember to assign runnersObj in the editor.

Comment
Add comment · Show 5 · 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 Joshua · Aug 09, 2011 at 01:49 AM 0
Share

The very first line won't even compile..

avatar image Josh 14 · Aug 09, 2011 at 02:01 AM 0
Share

Uh... yes it will, I just tested it. Why wouldn't it compile?

avatar image Eric5h5 · Aug 09, 2011 at 03:27 AM 0
Share

It would compile, but it's bad practice and results in slow code that's more error-prone. Always supply the type of variables (either explicitly, or implicitly with a value). Also, don't use quotes with GetComponent; it's slower and not type-safe.

avatar image Peter G · Aug 09, 2011 at 03:30 AM 1
Share

Your code should compile and work just fine with one exception.

The first line is dynamically typed so it won't compile on languages that require static typing. What's the type of RunnerControl? We don't know until you assign it. Even then you use duck typing to call SomeFunction(). Both techniques are legal in web player and on standalones because the code is JIT compiled so it can figure these things out at runtime, but on iOS and Android the code won't work.

Your code is more like traditional web js. Values have a type, but variables don't. They can store any type. It isn't necessarily bad, its just a little slower than static typing.

avatar image Eric5h5 · Aug 09, 2011 at 03:47 AM 1
Share

@Peter G: it will compile and work on iOS and Android. It doesn't have anything to do with JIT compilation. Just because it works, however, is not a good reason to do it. As I mentioned, it will be slow (not "a little" slower, more like 30-40X slower), and, since the type can be switched to anything, is more likely to result in coding mistakes.

avatar image
3

Answer by Joshua · Aug 09, 2011 at 01:22 AM

This is not only possible, this is advisable as this will cache the result of the GetComponent instead of looking it up each time.

Comment
Add comment · Show 1 · 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 aldonaletto · Aug 09, 2011 at 01:45 AM 0
Share

You can let the variable untyped, or (better) define its type as the script name (no quotes or extension - that's the type of the script in Unity):

var RunnerControl : RunnerControlScript; // <- this is the script type
RunnerControl = runnersObj.GetComponent(RunnerControlScript);
avatar image
0

Answer by Ted-Brown · Aug 09, 2011 at 04:01 AM

Thank you, everyone.

It sounds like one question has a resounding yes: you can assign the results of a GetComponent call to a variable, making it easier to read (and update, if necessary).

However, I'm seeing two different answers on its impact. Eric5h5 says it will be 30-40x slower, Joshua says it will be faster because the results are cached. Both make sense to me.

Is there a deeper answer to this?

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 Eric5h5 · Aug 09, 2011 at 04:04 AM 1
Share

Please don't post comments as answers. Also, you completely misunderstood what the comments are talking about. It's about the "var RunnerControl;" line, nothing whatsoever to do with GetComponent.

avatar image Ted-Brown · Aug 09, 2011 at 05:06 AM 0
Share

Err, O$$anonymous$$. Or perhaps you weren't clear in the first place. :P But thanks for clarifying.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing other scripts using GetComponent() 1 Answer

Running a script function using "GetComponent" 1 Answer

Custom _GetComponent( ) 2 Answers

NullReferenceException when setting Component as Variable, but no bugs 1 Answer

Getting all scripts attached to a gameObject (Problem) 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