- Home /
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
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>();
}
Thanks that worked. But when enemyAIScript.active = false the whole object becomes false rather than just the script.
Yes, that's because "active" is a (deprecated) property of the gameobject, not of a component. To disable a component, use:
enemyAIScript.enabled = false;
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.
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
probably my poor JS skills, try this:
var enemyUnit : GameObject;
var enemyAIScript : EnemyAI;
Or hope someone who's better at Java comes along :¬)
No problem, but did my update or the other answer fix your issue?
Your answer
Follow this Question
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