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 10, 2015 at 02:29 PM · javascriptenemyglobalgameobject.find

GameObject.Find doesn't work globally. If I put it inside a function it doesn't work either. How can I make it global ?

 var enemyUnit = GameObject.FindGameObjectWithTag("Enemy");
 var enemyAIScript = enemyUnit.gameObject.GetComponent(EnemyAI);
 
 function Start(){
 
 
 alert = false;
 caution = false;
 normal = true;
 }
 
 function Update(){
 
 if(alert == true && alertTimer >= 0){
         caution  = false;
         normal = false;
                 statusGUI.guiText.text = "ALERT : "+alertTimer;
         alertTimer -= Time.deltaTime;
         enemyAIScript.SendMessage("HuntTelic");
         enemyAIScript.go = false;
         
         
 }



The error it give in the editor is : UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function. ControlAI..ctor () (at Assets/BLAM/Enemies/RegularUnits/ControlAI.js:13)

.

If I put the variables in Start the update function doesn't recognize them. If I put it in update for some reason the rest of the script doesn't work . I don't have anywhere else to put it. What do I do ?

PS I didn't post the whole script. var enemyUnit is line 13 in the full script

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by tanoshimi · Jan 10, 2015 at 03:31 PM

Step 1: always type your variables:

 var enemyUnit : GameObject;
 var enemyAIScript : EnemyAI;

Step 2: Make sure that there is a gameobject tagged with "Enemy" active in your scene, and that it has an "EnemyAI" component attached. Then:

 function Start(){
   enemyUnit = GameObject.FindGameObjectWithTag("Enemy");
   enemyAIScript = enemyUnit.GetComponent.<EnemyAI>();
 }
Comment
Add comment · Show 2 · 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 Seventh Stealth · Jan 10, 2015 at 06:46 PM 0
Share

Thanks that worked. But when enemyAIScript.active = false the whole object becomes false rather than just the script.

avatar image tanoshimi · Jan 10, 2015 at 07:44 PM 0
Share

Yes, that's because "active" is a (deprecated) property of the gameobject, not of a component. To disable a component, use:

 enemyAIScript.enabled = false;
avatar image
0

Answer by Mmmpies · Jan 10, 2015 at 02:37 PM

Not too good at JS but you should declare the var before Start then assign it in Start

 var enemyUnit;
 var enemyAIScript; 
  
 function Start(){
 enemyUnit = GameObject.FindGameObjectWithTag("Enemy");
 enemyAIScript = enemyUnit.gameObject.GetComponent(EnemyAI);

At least that's what I'd do in C# so in theory it should work in JS. Although not 100% sure if you need the gameObject after enemyUnit on the last line.

Comment
Add comment · Show 4 · 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 Seventh Stealth · Jan 10, 2015 at 03:20 PM 0
Share

Tried that just now. Editor says NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value) ControlAI.Update () (at Assets/BLA$$anonymous$$/Enemies/RegularUnits/ControlAI.js:33).

I'm not sure if it's saying NullReferenceException because it can't find the script or because it doesn't know what the variable enemyAIScript

avatar image Mmmpies · Jan 10, 2015 at 03:28 PM 0
Share

probably my poor JS skills, try this:

 var enemyUnit : GameObject;
 var enemyAIScript : EnemyAI;

Or hope someone who's better at Java comes along :¬)

avatar image Seventh Stealth · Jan 10, 2015 at 06:19 PM 0
Share

Thanks anyway man

avatar image Mmmpies · Jan 10, 2015 at 06:43 PM 0
Share

No problem, but did my update or the other answer fix your issue?

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

26 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

Related Questions

Enemy death help 1 Answer

On TriggerEnter issues 1 Answer

how to delete multiple objects found with tags 2 Answers

Enemy following Player in range 2 Answers

Attack/Targeting Script Issue 1 Answer


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