x


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);
}
more ▼

asked Mar 14 '11 at 02:56 AM

networkZombie gravatar image

networkZombie
244 9 11 20

I'm not getting any errors from this script. What line exactly are you getting it from?

Mar 14 '11 at 03:10 AM AngryOldMan

UnityException: You are not allowed to call this function when declaring a variable. Move 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.

Mar 14 '11 at 04:25 PM networkZombie

doesnt tell me the line number

Mar 14 '11 at 04:46 PM networkZombie
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

This is just a guess, but can you really destroy an object, and then call the transform.positon of it?

more ▼

answered Mar 14 '11 at 05:32 AM

Dusinn gravatar image

Dusinn
3 1 1 3

Yes, because Destroy doesn't happen until the end of the frame.

Mar 14 '11 at 07:02 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3459
x3328
x1949
x822
x38

asked: Mar 14 '11 at 02:56 AM

Seen: 996 times

Last Updated: Mar 14 '11 at 03:07 AM