- Home /
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?
have you checked out this question?
http://answers.unity3d.com/questions/148949/assign-script-into-another-script-in-inspector.html
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.
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.
Uh... yes it will, I just tested it. Why wouldn't it compile?
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.
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.
@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.
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.
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);
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?
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.
Err, O$$anonymous$$. Or perhaps you weren't clear in the first place. :P But thanks for clarifying.
Your answer
![](https://koobas.hobune.stream/wayback/20220612173144im_/https://answers.unity.com/themes/thub/images/avi.jpg)