us of the "as" keyword (Instantiate, GetComponent)

Following tutorials I notice the use of the “as” keyword. I didn’t find it in the programing reference. Its usage seems rather self explanatory but would like to get more information for my own understanding;
I don’t like to write blindly without understanding nor being unable to find the reference

var player1 : GameObject = Instantiate(playerDeathObj, dpos, playerDeathObj.transform.rotation) as GameObject ;

The other exemple is more important as it seems that the “AS” Solved the issue I was encountering :

var script1 : shipController = player1.transform.gameObject.GetComponent("shipController") as shipController ;

Generally, the ‘as’ keyword performs a type conversion (a “cast”) that evaluates to null if the conversion fails (as opposed to throwing an exception). That way, you can do a "if (x != null) check in the next line and react depending on whether the cast succeeded or not.

The “as” keyword is used to cast the result to a specific type. However, in neither of those cases is it actually needed. Instantiate (in Unityscript) already returns the type of the prefab anyway, so it’s completely unnecessary in the first example. Just remove it, since it’s not doing anything.

var player1 : GameObject = Instantiate(playerDeathObj, dpos, playerDeathObj.transform.rotation);

In the second example, GetComponent should not use strings (in almost all cases). With strings, GetComponent returns Object, so it needs to be cast, but without strings, GetComponent returns the type of the component. So it should read

var script1 : shipController = player1.transform.gameObject.GetComponent(shipController);

(Although “player1.transform.gameObject” is superfluous. Just do “player1.GetComponent(shipController)”.)