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 Satamanster · Feb 10, 2013 at 12:53 AM · gameobjectfunction

Activating function on another object script - annoying...

Hello everyone, and here it goes.. Another boring scripting question... But i can't get this to work.. Oh god, when will i be able to program efficiently... Anyway.. I have my plane, an enemy plane and a dangerzone tagged as "dangerzone"...

My enemy i do already have a script to make him patrol a set of points inside my danger zone... i also have a script attached to my enemy with a pursue script and a third one which is controlling the enemy plane state... The patrol and chase scripts don't matter. My problem is with my state script.... I do have this function on my "Enemy_State_Control" script:

 #pragma strict
 
 var PursueScript: Pursue;
 
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 function startChase() {
 PursueScript.enabled = true;
 }

Now this is ok... What about in my own plane?

All it matter for the issue is this on my plane script, i do have this:

 var enemyPlane = GameObject.Find("f16_enemy");
 
 function OnTriggerEnter (other : Collider) {
     if (other.tag == "dangerzone")
     {
         enemyPlane.GetComponent("Enemy_State_Control").startChase();
     }
 }

So the pursue script starts as disabled and is supposed to turn on as my plane enters dangerzone... But my script plane is giving me this error:

"UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration."

What the hell am i doing wrong and why is not working... :( i don't deserve this.... Hope you guys can help me with your huge wisdom.... Thanks a lot...

Comment
Add comment · Show 4
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 AlucardJay · Feb 10, 2013 at 12:56 AM 0
Share

store the component you are trying to call the function on in a variable first.

 var enemyCtrlr : NameOfScript = enemyPlane.GetComponent("Enemy_State_Control");

then call the function from the stored reference :

 enemyCtrlr.startChase();


store the component you are trying to call the function on in a variable first.

 var enemyCtrlr : NameOfScript = enemyPlane.GetComponent("Enemy_State_Control");

then call the function from the stored reference :

 enemyCtrlr.startChase();

you can use GetComponent without using a String, just use the name of your script, so something like this :

 var enemyCtrlr : Enemy_State_Control = enemyPlane.GetComponent( Enemy_State_Control );
 enemyCtrlr.startChase();

Really, if there is only one Enemy_State_Control script, you should find it in the Start function :

 var enemyPlane : GameObject;
 var enemyStateCtrlr : Enemy_State_Control;
 
 function Start()
 {
     enemyPlane = GameObject.Find( "f16_enemy" );
     enemyStateCtrlr = enemyPlane.GetComponent( Enemy_State_Control );
 }
 
 function OnTriggerEnter (other : Collider) {
     if (other.tag == "dangerzone")
     {
         enemyStateCtrlr.startChase();
     }
 }
avatar image cdrandin · Feb 10, 2013 at 12:58 AM 0
Share

@alucardj that is correct, but I have done something like enemyPlane.GetComponent("Enemy_State_Control").startChase(); and works like a charm without Unity throwing exceptions. Try var enemyPlane : GameObject = GameObject.Find("f16_enemy").gameObject; and call it as you did. this has worked before for my projects, but I use C#, not sure if there is much of a difference in what is trying to be achieved.

avatar image AlucardJay · Feb 10, 2013 at 01:10 AM 0
Share

Well now I am very confused, you say C# but the example code is in uJS. Also am fairly sure variables can only be declared outside a function an can only be directly assigned values. To assign it by using a command like GetComponent it has to be done within a function.

avatar image Satamanster · Feb 10, 2013 at 01:45 AM 0
Share

@cdrandin altough alucardj answer solve my problem i do appreciate your contribution. I actually already had tried to as you said with no luck.... Anyway... Program$$anonymous$$g for begginers can really be a pain in the *ss... Thanks a lot for your help...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by shopguy · Feb 10, 2013 at 01:59 AM

Are you sure it is complaining about the startChase() line? I would think it is talking about this line:

var enemyPlane = GameObject.Find("f16_enemy");

Because that is the only line I see where you are declaring a variable and calling a function on the same line. I don't think you can change that to this though:

var enemyPlane; enemyPlane= GameObject.Find("f16_enemy");

..not without moving that inside a function, so probably just move it inside your Start() function (leave the 1st line outside, move 2nd line inside Start).

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

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

11 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

Related Questions

Array problem? help please! 1 Answer

How to call and run a function from another script on a transform. 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Message sender...how to know who send the message 1 Answer

DontDestroyOnLoad does not work after reload the scene? 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