- Home /
Scripting error!
I am getting 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." But the only function im calling while declaring a variable is Instantiate. Here's my script -
var target; //target = transform.Find("Character");
var damp : int = 3; var attackRange = 3; // safe distance from the player var followRange = 50; //distance required for enemy to start following var bulletPrefab : Transform; var savedTime = 0; var explosionPrefab : Transform;
var hitPoints = 3;
function Update () {
target = GameObject.FindWithTag("Player");
if((followRange > Vector3.Distance(target.transform.position, transform.position )) && (attackRange != Vector3.Distance(target.transform.position, transform.position ))) // if the enemy is closer than the follow range
{
follow(target);
}
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if ((attackRange >= Vector3.Distance(target.transform.position, transform.position )) &&(oddeven))
Shoot(seconds);
}
function follow(target) { if( attackRange < Vector3.Distance(target.transform.position, transform.position)) //if enemy is farther than attack range { transform.LookAt(target.transform); //look at target transform.Translate (0,0,6*Time.deltaTime, Space.Self ); //move towards target } }
function Shoot(seconds) { if(seconds != savedTime) { var shoot = Instantiate(bulletPrefab, transform.Find("BulletSpawn").transform.position, Quaternion.identity); shoot.gameObject.tag = "enemyProjectile"; shoot.rigidbody.AddForce(transform.forward * 1000);
savedTime = seconds;
}
}
function OnTriggerEnter(hit : Collider) { if(hit.gameObject.tag == "fallout") { Die(); }
if(hit.gameObject.tag == "playerProjectile")
{
hitPoints -= 1;
Destroy(hit.gameObject);
if(hitPoints < 1)
{
Die();
}
}
}
function Die() { Destroy(gameObject); var explode = Instantiate(explosionPrefab, gameObject.transform.position, Quaternion.identity); }
I'm not getting any errors from this script. What line exactly are you getting it from?
UnityException: You are not allowed to call this function when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. I'm getting this error but i dont call any function while declaring a variable except for Instantiate.
Answer by Dusinn · Mar 14, 2011 at 05:32 AM
This is just a guess, but can you really destroy an object, and then call the transform.positon of it?
Yes, because Destroy doesn't happen until the end of the frame.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112653im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
create variable trouble 1 Answer
Javascript not being updated, variables being overridden, but C# is fine 1 Answer
Assets/Standard Assets/Scripts/General Scripts/follow.js(9,59): BCE0044:unexpected char: 0xAD 1 Answer
When i call some variables to an other second script i have a problem 1 Answer
Accessing A Variable From Another Script 4 Answers