- Home /
Coding technique question
Looking at the AngryBot demo I stumbled apon something I think I understand what it does but don't really know the why and what.
They have the variable declaration:
private var raycast : PerFrameRaycast;
They then initialise it on awake thus:
raycast = GetComponent.< PerFrameRaycast > ();
And use it:
var hitInfo : RaycastHit = raycast.GetHitInfo ();
So my first question is the syntax of 2). I'm not familiar with it. I'm gessing it creates an instance of the object (script) ? Looks like its a seperate thread in its own right?. I think I follow what's it doing but not 100% sure.
Secondly any good references to see what means in JS?
(grrr sorry for the constant edits the "" are messing the formatting)
Answer by Eric5h5 · Apr 25, 2012 at 10:18 PM
GetComponent.<PerFrameRaycast>()
is the generic version of GetComponent; it works the same as C# except for slightly different syntax. However it's actually better in JS not use use generics with GetComponent, as long as you're using Unity 3.4 and later. Just do raycast = GetComponent(PerFrameRaycast)
. In any case, no, it has nothing to do with threads.
Ah ok. I guess I was wondering if it was its own thread instance because it was doing its own Update() but that probably shows my ignorance on the inner workings of Unity.
Answer by FizzyBear · Apr 25, 2012 at 09:27 PM
private var raycast : PerFrameRaycast;
This code just creates a raycast (a straight line within the range you've determined). But this is created per frame rather than just once (the answer is in the code you provided). You can also reference it to this link;
http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html
var hitInfo : RaycastHit = raycast.GetHitInfo ();
GetComponent means it's referencing to the inspector ingame, where ever you've assigned that script to. (whatever is in here) is the script name. You can use it in the format as:
variablenamehere = GetComponent(Blahblahblah).avariablewithin
So you're almost correct when you're creating an instance of the object (it's just literally calling it).
When you say < xxx >, what are you referring to? Please be more specific if you can.
Hope the parts I've answered or attempted to answer helped.
Your answer
![](https://koobas.hobune.stream/wayback/20220613070747im_/https://answers.unity.com/themes/thub/images/avi.jpg)