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.

  1. They have the variable declaration:

    private var raycast : PerFrameRaycast;
  2. They then initialise it on awake thus:

    raycast = GetComponent.< PerFrameRaycast > ();
  3. 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 < xxx > means in JS?

(grrr sorry for the constant edits the “<” “>” are messing the formatting)

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.

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;

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.