Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 eeveelution8 · May 04, 2014 at 10:14 PM · javascripterrorupdatefunctionlag

Script is causing immense lag, and I don't know what's causing it.

I have zombies in a game that fire mini projectiles at players, and have health scripts as well. I know it's one of their scripts because adding them to the game causes it to quickly become laggy, and then unplayable.

this is the HP script,

 var HP : int = 5;
 var body : GameObject;
 var speed : int = 5;
 
 
 function Start () {
 body = gameObject;
 }
 
 function Update () {
 
 transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
 
     if(HP < 1) {
         var instance : GameObject = Instantiate(body, transform.position, transform.rotation);
         DestroyObject (gameObject);
     }
 }
 
 function OnCollisionEnter (myCollision : Collision) {
     
     if(myCollision.gameObject.name == "zombieclimb"){
         
         transform.Translate(Vector3(0,1,0) * Time.deltaTime);
         
     }
 }


and the shooting script,

 var projectile : GameObject;
 
 var startdelay = 0.1;
 var delay = 0.1;
 
 InvokeRepeating("LaunchProjectile", startdelay, delay);
 
 
 function LaunchProjectile () {
     
     var instance : GameObject = Instantiate(projectile, transform.position, transform.rotation);
         
 }
     
     
     
 function Start () {
 projectile = gameObject;
 }

Again, I don't know what is causing it, but I suspect it may be the Update function. Also, nothing appears in the console either.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by SeeSharp · May 04, 2014 at 10:19 PM

Hi,

If you start lagging after you shoot for a while, then add this piece of code to your 'LaunchProjectile' funtion: Destroy(instance, 10)

That will destroy the instantiated bullet after 10 seconds.

Other than that, everything looks fine to me and I cannot locate anything else that might lag your game right now.

Please ask if you need further assistance.

Best of luck,

SeeSharp.

Comment
Add comment · Show 2 · 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 eeveelution8 · May 05, 2014 at 12:04 AM 0
Share

I changed the script to this,

 var projectile = gameObject;
 
 var startdelay = 0.1;
 var delay = 0.1;
 
 InvokeRepeating("LaunchProjectile", startdelay, delay);
 
 
 function LaunchProjectile () {
     
     var instance : GameObject = Instantiate(projectile, transform.position, transform.rotation);
         
 }

And it no longer lags. But it comes up with a new error,

UnityException: You are not allowed to call this function when declaring a variable. $$anonymous$$ove 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, Ins$$anonymous$$d move initialization to the Awake or Start function. enemyshoot..ctor () (at Assets/scripts/enemyshoot.js:1)

It's why i changed it to the start function and went wrong, but I dont know how to fix it from there.

avatar image eeveelution8 · May 05, 2014 at 12:10 AM 0
Share

I got it, it reads as,

 var projectile : GameObject;
 
 var startdelay = 0.1;
 var delay = 0.1;
 
 InvokeRepeating("LaunchProjectile", startdelay, delay);
 
 
 function LaunchProjectile () {
     
     var instance : GameObject = Instantiate(projectile, transform.position, transform.rotation);
         
 }


and works now.

avatar image
1

Answer by Kiloblargh · May 04, 2014 at 10:28 PM

The InvokeRepeating() shouldn't even work, as it is outside a function. You can declare variables outside a function but not do anything else.

If it did work, it looks like the object with this "shooting script" on it would copy itself 10 times a second, and all of those copies would make copies of themselves 10 times a second, exponentially overflowing your scene with instances and probably crashing the game.

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 eeveelution8 · May 04, 2014 at 11:58 PM 0
Share

the delay variables are changed in the inspector, for most of the zombies, I have it set to a start delay of 0, and delay of 3.

And, you're right, I started the scene, and was able to pause it quickly enough to see that indeed it was spawning clones of itself.

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.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Incremental game need help 1 Answer

How to crouch without falling through floor? 2 Answers

Performance Optimization ~Function Update: Loop or Once ? 5 Answers

Trouble making money system 1 Answer

Expecting ( found Update (Javascript) 1 Answer


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