Search Unity

Question i have a performance problem in my code

Discussion in 'Scripting' started by kalhach, Jun 24, 2021.

  1. kalhach

    kalhach

    Joined:
    Aug 26, 2020
    Posts:
    42
    hi good day, i recently found out that my game was lagging and at the moment i didn't know why... but a little bit of researching in the manuals and the diagnosis performance problem tutorial in unity i found why my game was lagging
    upload_2021-6-24_17-21-14.png

    one of the reasons falls on the update of my script "Mapmanager.update"
    i searched in my script library and found that there were many variables but also saw that in the update there were not so many methods.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Tilemaps;
    5.  
    6. public class MapManager : MonoBehaviour
    7. {
    8.     GameObject Cube;
    9.     SimpleJump CubeVarible;
    10.     GameObject Simplejumpe;
    11.     SimpleJump Simplejump1;
    12.     Vector3Int gridposition1_3;
    13.     Vector3Int gridposition1_4;
    14.     Vector3Int gridposition1_5;
    15.     Vector3Int gridposition1_6;
    16.     public GameObject firstGrid1;
    17.     public GameObject firstGrid2;
    18.     public GameObject firstGrid3;
    19.     public GameObject firstGrid4;
    20.     public GameObject firstGrid5;
    21.     public GameObject firstGrid6;
    22.     public GameObject firstGrid7;
    23.     public GameObject firstGrid8;
    24.     public GameObject firstGrid9;
    25.     public GameObject firstGrid10;
    26.     public GameObject secondGrid1;
    27.     public GameObject secondGrid2;
    28.     public GameObject secondGrid3;
    29.     public GameObject secondGrid4;
    30.     public GameObject secondGrid5;
    31.     public GameObject secondGrid6;
    32.     public GameObject secondGrid7;
    33.     public GameObject secondGrid8;
    34.     public GameObject secondGrid9;
    35.     public GameObject secondGrid10;
    36.     public GameObject thirdGrid1;
    37.     public GameObject thirdGrid2;
    38.     public GameObject thirdGrid3;
    39.     public GameObject thirdGrid4;
    40.     public GameObject thirdGrid5;
    41.     public GameObject thirdGrid6;
    42.     public GameObject thirdGrid7;
    43.     public GameObject thirdGrid8;
    44.     public GameObject thirdGrid9;
    45.     public GameObject thirdGrid10;
    46.     public GameObject forthGrid1;
    47.     public GameObject forthGrid2;
    48.     public GameObject forthGrid3;
    49.     public GameObject forthGrid4;
    50.     public GameObject forthGrid5;
    51.     public GameObject forthGrid6;
    52.     public GameObject forthGrid7;
    53.     public GameObject forthGrid8;
    54.     public GameObject forthGrid9;
    55.     public GameObject forthGrid10;
    56.     public Tilemap FirstGrid3;
    57.     public Tilemap FirstGrid4;
    58.     public Tilemap FirstGrid5;
    59.     public Tilemap FirstGrid6;
    60.     public Tilemap SecondGrid03;
    61.     public Tilemap SecondGrid04;
    62.     public Tilemap SecondGrid05;
    63.     public Tilemap SecondGrid07;
    64.     public GameObject positionerchecker;
    65.     public bool gravon;
    66.     void Awake()
    67.     {
    68.         Cube = GameObject.FindGameObjectWithTag("Player");
    69.         CubeVarible = Cube.GetComponent<SimpleJump>();
    70.     }
    71.  
    72.      void Update()
    73.     {
    74.         firstLevel();
    75.         AutoJump();
    76.         AntiGrav();
    77.         TileMapPosition();
    78.         gridposition1_3 = FirstGrid3.WorldToCell(CubeVarible.transform.position);
    79.         gridposition1_5 = FirstGrid5.WorldToCell(CubeVarible.transform.position);
    80.     }
    81.     public void firstLevel()
    82.     {
    83.      if(CubeVarible.transform.position.x >= 80)
    84.      {
    85.             firstGrid2.SetActive(true);
    86.      }
    87.      if(CubeVarible.transform.position.x >= 185)
    88.      {
    89.             firstGrid1.SetActive(false);
    90.             firstGrid3.SetActive(true);
    91.      }
    92.    
    93.      if(CubeVarible.transform.position.x >= 310)
    94.      {
    95.             firstGrid2.SetActive(false);
    96.             firstGrid4.SetActive(true);
    97.      }
    98.      if(CubeVarible.transform.position.x >= 420)
    99.      {
    100.             firstGrid3.SetActive(false);
    101.             firstGrid5.SetActive(true);
    102.      }
    103.      if (CubeVarible.transform.position.x >= 540)
    104.      {
    105.             firstGrid4.SetActive(false);
    106.             firstGrid6.SetActive(true);
    107.         }
    108.      if (CubeVarible.transform.position.x >= 645)
    109.      {
    110.             firstGrid5.SetActive(false);
    111.             firstGrid7.SetActive(true);
    112.      }
    113.  
    114.      if (CubeVarible.transform.position.x >= 760)
    115.      {
    116.             firstGrid6.SetActive(false);
    117.             firstGrid8.SetActive(true);
    118.      }
    119.      if (CubeVarible.transform.position.x >= 885)
    120.      {
    121.             firstGrid7.SetActive(false);
    122.             firstGrid9.SetActive(true);
    123.      }
    124.      if (CubeVarible.transform.position.x >= 1000)
    125.      {
    126.             firstGrid8.SetActive(false);
    127.             firstGrid10.SetActive(true);
    128.      }
    129.     }
    130.     void secondLevel()
    131.     {
    132.  
    133.     }
    134.     void thirdLevel()
    135.     {
    136.  
    137.     }
    138.  
    139.     void AutoJump()
    140.     {
    141.         if (CubeVarible.groundnear == true)
    142.         {
    143.             if (gridposition1_3 == new Vector3Int(23, -3, 0))
    144.             {
    145.                 CubeVarible.jumpvel =10.5f;
    146.                 CubeVarible.jump();
    147.  
    148.             }
    149.             else if (gridposition1_3 == new Vector3Int(95, -1, 0))
    150.             {
    151.                 CubeVarible.jumpvel = 10.5f;
    152.                 CubeVarible.jump();
    153.             }
    154.             else if (gridposition1_3 == new Vector3Int(103, -1, 0))
    155.             {
    156.                 CubeVarible.jumpvel = 10.5f;
    157.                 CubeVarible.jump();
    158.             }
    159.             else if (gridposition1_3 == new Vector3Int(111, -1, 0))
    160.             {
    161.                 CubeVarible.jumpvel = 10.5f;
    162.                 CubeVarible.jump();
    163.             }
    164.             else if (gridposition1_3 == new Vector3Int(119, -1, 0))
    165.             {
    166.                 CubeVarible.jumpvel = 10.5f;
    167.                 CubeVarible.jump();
    168.             }
    169.             else
    170.             {
    171.                 CubeVarible.jumpvel = 6.5f;
    172.             }
    173.  
    174.         }
    175.     }
    176.    
    177.     public void AntiGrav()
    178.     {
    179.         var saveBox = SaveBox.Instance;
    180.  
    181.         if (CubeVarible.groundnear == true)
    182.         {
    183.             if (gridposition1_4 == new Vector3Int(97, -3, 0))
    184.             {
    185.                 CubeVarible.rb.gravityScale *= -1;
    186.             }
    187.             if (gridposition1_5 == new Vector3Int(-50, -1, 0))
    188.             {
    189.                 CubeVarible.rb.gravityScale *= -1;
    190.             }
    191.             if (gridposition1_5 == new Vector3Int(-25, 1, 0))
    192.             {
    193.                 CubeVarible.rb.gravityScale *= -1;
    194.             }
    195.             if (gridposition1_5 == new Vector3Int(-6, 0, 0))
    196.             {
    197.                 CubeVarible.rb.gravityScale *= -1;
    198.             }
    199.             if (gridposition1_6 == new Vector3Int(3, -3, 0))
    200.             {
    201.                 CubeVarible.rb.gravityScale *= -1;
    202.             }
    203.             if (gridposition1_6 == new Vector3Int(29, 0, 0))
    204.             {
    205.                 CubeVarible.rb.gravityScale *= -1;
    206.             }
    207.         }
    208.  
    209.     }
    210.  
    211.     public void TileMapPosition()
    212.     {
    213.         if (CubeVarible.rb.transform.position.x >= 210)
    214.         {
    215.             gridposition1_3 = FirstGrid3.WorldToCell(CubeVarible.transform.position);
    216.         }
    217.         if (CubeVarible.rb.transform.position.x >= 334)
    218.         {
    219.             gridposition1_4 = FirstGrid4.WorldToCell(CubeVarible.transform.position);
    220.         }
    221.         if (CubeVarible.rb.transform.position.x >= 450)
    222.         {
    223.             gridposition1_5 = FirstGrid5.WorldToCell(CubeVarible.transform.position);
    224.         }
    225.         if (CubeVarible.rb.transform.position.x >= 540)
    226.         {
    227.             gridposition1_6 = FirstGrid6.WorldToCell(CubeVarible.transform.position);
    228.         }
    229.     }
    230. }
    here is my code (the culprit) so my question is what could it be the problem or do you have any idea of what is happening? thanks in advance
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Let me observe that you should open up your character stats page and spend some points in the category marked "Learning about C# collections." :)

    All kidding aside, this needs to be arrays and loops, but at least that is not going to be the source of the slowdown. Making it arrays and loops will just make the above code into about 20 lines max.

    NOW... what is going on in places like the
    .jump()
    method?

    And also, how many of these script instances do you have in the scene?
     
    Lekret likes this.
  3. kalhach

    kalhach

    Joined:
    Aug 26, 2020
    Posts:
    42
    the jump() method is this
    Code (CSharp):
    1.     public void ishitsideways()
    2.     {
    3.         if (isHittedside())
    4.         {
    5.             StartCoroutine(ParticleDeadCoroutine());
    6.         }
    7.         else
    8.         {
    9.             //nothing
    10.         }
    11.        
    12.     }
    all right i have 2 scenes use this same script fjust for one scene and only in 3 things, 2 on click events and one alocating the script and in the second scene
     
  4. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Enable deep profiling and dig deeper.