UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration

Hi everybody! I’m new to game development and programming and I’ve got a little problem.

#pragma strict

var otherObj = gameObject; // The sword
var Damage : int = 50;

function OnCollisionEnter (hit : Collision)
{
	if (otherObj.animation["Attack"].enabled)
	{
		if(hit.gameObject.tag == "enemy")
		{
			hit.gameObject.SendMessage("ApplyDamage", Damage);
		}	
	}
}

The code seems to be working but I always get an error (UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.).

What is wrong and what should I do?

I tried reading answers on similar questions but I could barely understand them. Please, explain it in simple words so that a newbie like me will understand. :slight_smile:

var otherObj = gameObject; // ?

try

var otherObj : GameObject; // :)

You are trying to dynamically create a variable of type GameObject and make it equal to the gameObject the script is attached to.

#pragma strict won’t let you do that. Cuz its strict mode.

Also, When you use a capital letter for something like GameObject or Transform, it refers to the Type. So, ‘of this Type’.

edit : See Bunny83’s corrections below. All thumbs up go there :wink: