Search Unity

Optimization FPS in my game?

Discussion in 'Scripting' started by hellpirat, Aug 2, 2015.

  1. hellpirat

    hellpirat

    Joined:
    Jun 12, 2015
    Posts:
    26
    Hello.
    I have a couple of problems in my application.
    I have a not standard List with 36 elements in it.

    Code (CSharp):
    1.    
    2. public class ShopScript : MonoBehaviour{
    3.  
    4. public void BuyRocket(int num)
    5.     {
    6.         for (var i = 0; i < Itemses.Count; i++)
    7.         {
    8.             Itemses[i].ActiveRocket.SetActive(false);
    9.  
    10.             if (num == i)
    11.             {
    12.                 Itemses[i].ActiveRocket.SetActive(true);
    13.                 Debug.Log(Itemses[i].Price);
    14.             }
    15.             Debug.Log("items " + Itemses[i]);
    16.         }
    17.  
    18.     }
    19.  
    20. }
    21. [System.Serializable]
    22. public class Items
    23. {
    24.     public GameObject Prefabs;
    25.     public int Price;
    26.     public bool IsBought;
    27.     public GameObject ActiveRocket;
    28.  
    29. }
    This script I attached on some Button. When I click on them - my fps drops on <20
    Code (CSharp):
    1.  
    2.  
    3. public class BuyItemScript : MonoBehaviour
    4. {
    5.  
    6.     public ShopScript ShopScript;
    7.     public int RocketIndex;
    8.    
    9.  
    10.     void Start()
    11.     {
    12.         var itemScript = GameObject.Find("ShopScript");
    13.         ShopScript = itemScript.GetComponent<ShopScript>();
    14.     }
    15.  
    16.     public void BuyRocket()
    17.     {
    18.         ShopScript.BuyRocket(RocketIndex);
    19.     }
    20. }
    21.  
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Debug.Log is a very slow operation.
     
    hellpirat and Kiwasi like this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This. Remember debug.log is not just putting a string on the console, it's also doing a full stack trace. This gets expensive, especially in loops.
     
    hellpirat and hippocoder like this.
  4. hellpirat

    hellpirat

    Joined:
    Jun 12, 2015
    Posts:
    26
    Thanks for the help, after removing debug.log everything was working perfectly.