Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Seventh Stealth · Jan 09, 2015 at 11:02 PM · javascriptgetcomponent

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seventh Stealth · Jan 09, 2015 at 11:14 PM 0
Share

plez help

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Jan 10, 2015 at 12:53 AM 0
Share

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.

avatar image Paulius-Liekis · Jan 10, 2015 at 01:36 AM 0
Share

You can do explicit typing - sure. But generally speaking Javascript does more things for you (and occasionally shoots your foot). For example:

  1. function Start() can mean two things in Javascript (coroutine and regular void function). In C# there is no such ambiguity.

  2. 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! :)

avatar image Eric5h5 · Jan 10, 2015 at 03:31 AM 1
Share

enemyUnits.GetComponent doesn't compile in JS either. ;) You can write function Start () : void or function Start () : IEnumerator if you want.

avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Paulius-Liekis · Jan 10, 2015 at 01:39 AM 0
Share

I don't think it's his actual script, since line number in the error doesn't match actual pasted functions.

avatar image Eric5h5 · Jan 10, 2015 at 03:32 AM 0
Share

I think it's part of the script. Also it's a fairly common error.

avatar image Seventh Stealth · Jan 10, 2015 at 10:47 AM 0
Share

It is my script. I only posted the bit i was getting an error for. Thanks Eric!

avatar image Seventh Stealth · Jan 10, 2015 at 11:53 AM 0
Share

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

avatar image Eric5h5 · Jan 10, 2015 at 03:59 PM 0
Share

All code must be inside a function. You should only declare variables globally.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object pool with "Null Reference Exception" problem 1 Answer

create GUI.Label and then access to it.. 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

grab a variable off of a script of a child of an object? 1 Answer

ren]er cube problem 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges