• 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 networkZombie · Mar 14, 2011 at 02:56 AM · javascripterrorvariabledeclaration

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 &lt; 1)
     {
         Die();
     }
 }

}

function Die() { Destroy(gameObject); var explode = Instantiate(explosionPrefab, gameObject.transform.position, Quaternion.identity); }

Comment
Add comment · Show 3
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 AngryOldMan · Mar 14, 2011 at 03:10 AM 1
Share

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

avatar image networkZombie · Mar 14, 2011 at 04:25 PM 0
Share

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.

avatar image networkZombie · Mar 14, 2011 at 04:46 PM 0
Share

doesnt tell me the line number

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Show 1 · 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
avatar image Eric5h5 · Mar 14, 2011 at 07:02 AM 0
Share

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Unity mathematics phrasing. 1 Answer

functions file and NullReferenceException: Object reference not set to an instance of an object 1 Answer

Scripting error #2! 2 Answers

gameObject.active Find 1 Answer

Do something when something happens in another script. 3 Answers


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