• 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 /
  • Help Room /
avatar image
0
Question by pavlov64 · Jan 05 at 01:32 PM · noobproblem during runtimelaggy

why does attaching a script to unity makes the game go laggy?

I have a script for a homing missile. When you shoot one it finds a game object with an enemy tag, if there's no enemy tag it explodes. When I start unity it works as intended, until I attach a script. When I shoot a missile and there's no enemy tag it goes extremely laggy (like 0,002 fps). If Unity is restarted it works again. Why does this happen?,Ok so, I made a script for a homing missile. When you shoot one it checks if there's any game object with an enemy tag. If you shoot a missile when there's no enemy tag in the scene it explodes. If I play the game without attaching the script it works as intended, if I do it, the game gets extremely laggy (like 0.002 fps) when there's no enemy tag. When unity is restarted it works again, until I attach any script.

Comment
Add comment · Show 4
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 Hellium · Jan 05 at 01:37 PM 0
Share

If the script is responsible for the issue, how can we help without seeing it?

avatar image pavlov64 Hellium · Jan 05 at 03:47 PM 0
Share

I'm not sure if the script is the problem since it works perfectly when you restart. But here is it anyway.

     Rigidbody2D rb;
     Transform target;
     public float rotateSpeed = 200f;
     
     IEnumerator WinBlock()
     {
         GetComponent<Animator>().SetBool("explode", true);
         yield return new WaitForSeconds(0.5f);
         Destroy(this.gameObject);
     }
 
     void Start()
     {
         if (!(GameObject.FindGameObjectsWithTag("enemy").Length == 0))
         {
             target = GameObject.FindGameObjectWithTag("enemy").transform;
 
         }
 
         rb = this.gameObject.GetComponent<Rigidbody2D>();
         transform.Rotate(0, 0, -90); // this makes the missile spawn horizontally
     }
     private void FixedUpdate()
     {       
         Vector2 direction = (Vector2)target.transform.position - rb.position;
         direction.Normalize();
         float rotationDir = Vector3.Cross(direction, transform.up).z;
         rb.angularVelocity = -rotateSpeed * rotationDir;
         rb.velocity = rb.transform.up * 8;
     }
     private void Update()
     {
         //if there's no enemy tag the missile explodes
 
         if (GameObject.FindGameObjectsWithTag("enemy").Length == 0)
         { 
             StartCoroutine(WinBlock());
         }
     }

avatar image Hellium pavlov64 · Jan 05 at 04:00 PM 1
Share

Calling FindGameObjectsWithTag and creating a new coroutine every frame for 0.5 seconds can be the cause of your problem, especially at high FPS.


You should have a custom class listing your enemies and a flag to not call StartCoroutine if WinBlock is already running.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

179 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 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 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 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 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 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 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 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 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

Make a rigidbody Jump (global up) 3 Answers

Unity Noob - clicking on video textured object to start the video 0 Answers

C# GetKeyDown not working 3 Answers

Fancy camera animation but still follower player? 0 Answers

Move Foward key W sometimes keeps walking after I let go of W, only happens afew times 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