How can I change the layer of a GameObject?

I looked at so I did this

var theEnemy = GameObject; 

function Attack ()
      theEnemy.layer = 8;

I get the error

Assets/Scripts/Weapons/RayCastCloseCombat.js(59,26): BCE0019: ‘layer’ is not a member of ‘System.Type’.

I’m not sure why it did not work.

PS I’m changing the layer of the enemies so that when the player attacks the knife will go through the enemy not draw on top of it.


var theEnemy = GameObject;


var theEnemy : GameObject;

The reason why your old code didn’t work is because you haven’t specified theEnemy as a GameObject correctly; therefore, Unity is treating it as a general javascript variable, which isn’t what you want.