Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity game sometimes runs verrry slow, fine otherwise.

Discussion in 'Scripting' started by EventHorizon67, Dec 4, 2015.

  1. EventHorizon67

    EventHorizon67

    Joined:
    Nov 17, 2014
    Posts:
    2
    So I've been working on a game for the past couple of months (my first try at a full Unity game) and suddenly, and only sometimes, when I start the game, it will run at 1 fps. Otherwise it runs at 500+. So I ran the profiler and played the game about 6 or 7 times to get the slowness to re-appear, and it finally did. Upon checking the profiler, I noticed this:



    Here is the function being referenced:
    Code (csharp):
    1.  
    2. void OnTriggerEnter2D(Collider2D c)
    3.   {
    4.   if (c.CompareTag("Win Condition"))
    5.   {
    6.   c.gameObject.GetComponent<SpriteRenderer>().color = Color.black;
    7.   }
    8.   else if (c.CompareTag("Respawn"))
    9.   {
    10.   lastSpawn = c.gameObject;
    11.   }
    12.   else if ((c.CompareTag("Boundary")))
    13.   {
    14.   Respawn();
    15.   }
    16.   }
    17.  
    All Respawn(); does is call this Coroutine that my friend wrote for respawning:
    Code (csharp):
    1.  
    2. IEnumerator Explosion()
    3.   {
    4.   hook.CancelHook();
    5.   playerSprite.gameObject.SetActive(false);
    6.   player.velocity = Vector2.zero;
    7.   player.isKinematic = true;
    8.   //ParticleSystem Death = Instantiate(deathParticle, transform.position, Quaternion.identity) as ParticleSystem;
    9.   yield return new WaitForSeconds(respawnDelay);
    10.   transform.position = lastSpawn.transform.position;
    11.   playerSprite.gameObject.SetActive(true);
    12.   player.isKinematic = false;
    13.   //Destroy(Death.gameObject);
    14.   OnRespawn();
    15.   }
    16.  
    OnRespawn(); is an Event that sends a respawn event off to relevant scripts, but I commented it out and I still got the slowness, so does anyone have any idea why my game only occasionally runs super slowly? It happens all the time on the Web Player, and occasionally (tested with multiple machines).

    Edit: wow the forum killed my file indents!
     
    Kerith likes this.
  2. EventHorizon67

    EventHorizon67

    Joined:
    Nov 17, 2014
    Posts:
    2
    I solved my issue. Turns out my Polygon collider was sometimes checking for collisions very far away from the player, causing it to do mass amounts of physics calls. I switched to a set of primitive colliders and the game runs fine now.