Help. GetComponent(Script) not returning anything - Missing method exception

function Start(){
var enemyUnits = GameObject.FindGameObjectsWithTag(“Enemy”);
var enemyAIScript = enemyUnits.GetComponent(EnemyAI);

alert = false;
caution = false;
normal = true;
}

function Update(){

		enemyAIScript.SendMessage("Hunt");
		enemyAIScript.go = false;
}

Hi. Admittedly this is a repost but my last post got no answers on it so oh well. I’ve go this script ^^^ but when I play it in the editor enemyAIScipt returns nothing. A script called EnemyAI is definetely attached to it but it still doesn’t return anything. I got this error in the editor

MissingMethodException: UnityEngine.GameObject.GetComponent
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher ()
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.Create ()
Boo.Lang.Runtime.RuntimeServices.DoCreateMethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object args)
Boo.Lang.Runtime.RuntimeServices.CreateMethodDispatcher (System.Object target, System.String name, System.Object args)
Boo.Lang.Runtime.RuntimeServices+c__AnonStorey15.<>m__9 ()
Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object args)
UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object args, System.Type scriptBaseType)
ControlAI.Start () (at Assets/BLAM/Enemies/RegularUnits/ControlAI.js:18)

I think I’ using GetComponent wrong but the unity tutorials for JS don’t discuss getting scripts so I’m not sure. Help

var enemyUnits = GameObject.FindGameObjectsWithTag(“Enemy”);

It’s plural (both your variable and FindGameObjectsWithTag) - returns an array of GameObjects. Then you try to call GetComponent on array and array doesn’t have such method - hence the exception.

If you’re actually want to call method on multiple objects, then you will have to iterate over enemyUnits and call on each individually.

BTW: In order to call SendMessage, I don’t think you need a MonoBehavior. You can call it on GameObject directly, can’t you?

Side note:

Fuck JavaScript - switch to C#. You will shoot yourself in a foot again. I promise.
And use strict types (i.e. no var) - you will get a compile error
instead, much easier to understand, you get it much earlier and you will fix it sooner.
Well…at least until you understand what you’re writing.

You’re defining enemyAIScript in Start, and then trying to use it outside of Start, where it doesn’t exist. If you have a global enemyAIScript variable, that’s entirely separate from the new one you defined in Start. So use the global one instead of making a new one.