- Home /
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...
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();
}
}
@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.
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.
@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...
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).
Your answer

Follow this Question
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