- Home /
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
Answer by Paulius-Liekis · Jan 09, 2015 at 11:20 PM
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 realize you can explicitly type in JS, and you can implicitly type in C#, right? The language you use has little to do with shooting yourself in the foot—it's not paying attention to what you're doing that does that.
You can do explicit typing - sure. But generally speaking Javascript does more things for you (and occasionally shoots your foot). For example:
function Start()
can mean two things in Javascript (coroutine and regular void function). In C# there is no such ambiguity.enemyUnits.GetComponent wouldn't compile in C# since, var (line above) would resolve into
object[]
, and array doesn't have a method GetComponent, hence compile error. Generally speaking Javascript resolves functions in runtime when it must, C# does this during compile time.
So I'll still say the same - make compiler do work for you, unless you know what you're doing.
This saying about shooting yourself in a foot is an old one and has a lot to do with languages. To quote Bjarne Stroustrup:
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off and a kid near by.
Peace! :)
enemyUnits.GetComponent doesn't compile in JS either. ;) You can write function Start () : void
or function Start () : IEnumerator
if you want.
Answer by Eric5h5 · Jan 10, 2015 at 12:54 AM
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.
I don't think it's his actual script, since line number in the error doesn't match actual pasted functions.
I think it's part of the script. Also it's a fairly common error.
It is my script. I only posted the bit i was getting an error for. Thanks Eric!
Wait a second. I've put both variables in globally but now it tells me I can't use GameObject.Find globally. If I put it back into start or update I get the same problem as before where it's local and the rest of the script does't recognize it. So it doesn't let me make it global and if I make it local it doens't work. What do I do
All code must be inside a function. You should only declare variables globally.