placement of scripts/ multiple functions (Enemy AI)

i’ve got a problem relating to enemy AI- I want my enemies to fire a sphere at the player once they have detected them (I have script already to look at the player once they are in range, and not hidden behind walls etc- raycast)

however i’m finding it difficult to implement a shoot script, here’s what i have so far:

this brings error code: ArgumentException: get_time can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
EnemyAttack02…ctor () (at Assets/Scripts/EnemyAttack02.js:33)

var LookAtTarget : Transform;
var range = 100.0;
var damp = 5.0;
var bullet : Transform;
var Spawnpoint:GameObject;
var savedTime = 0;

function Update()
{
	if(InAttackRange())
	{
		var targetRotation = Quaternion.LookRotation (LookAtTarget.position - transform.position, Vector3.up);
	transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * damp);
	}
}
function InAttackRange()
{
	if(Vector3.Distance (transform.position, LookAtTarget.position) > range)
	{
		return false;
	}
	var hit : RaycastHit;
	if (Physics.Linecast (transform.position, LookAtTarget.position, hit))
	{
		if (hit.collider.gameObject.tag != "Player")
		{
			return false;
		}
	}
	return true;
	
}
var seconds : int = Time.time;
var oddeven = (seconds % 2);

if(oddeven)
{
Shoot(seconds);
}
function Shoot(seconds)
{
if(seconds!=savedTime)
{
var bullet = Instantiate(bullet ,transform.Find("BulletSpawn").transform.position ,Quaternion.identity);

bullet.rigidbody.AddForce(transform.forward * 10);
savedTime=seconds;
}

}

You can’t initialize “seconds” to “Time.time” as part of the class declaration. You need to do it in its awake/start calls. Similarly, “oddeven” needs to wait until seconds is initialized.:

var seconds : int;
var oddeven : int;

function Start()
{
seconds = Time.time;
oddeven = (seconds % 2);
}

However, this whole block of code:

var seconds : int = Time.time;
var oddeven = (seconds % 2);

if(oddeven)
{
Shoot(seconds);
}

is not even in a function. Is this misplaced?

possibly!

basically i’m a nub when it comes to scripting so i’ve been trying to paste bits and pieces of script and help from people together

What i’m trying to do there is get the object to fire (once the raycast confirms a collision with the player) but only once every 2 or so - a fairly slow pace.

which is where my lack of knowledge is coming undone!