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. Dismiss Notice

Resolved Lag on first ontriggerenter2d

Discussion in 'Scripting' started by plmahdi, Oct 1, 2023.

  1. plmahdi

    plmahdi

    Joined:
    May 6, 2017
    Posts:
    2
    I'm working on my first commercial game and I countered a weird bug which is not logical(to me).
    When the game begins on the first trigger (OnTriggerEnter2d) game lags for a second.

    The scene has a few gameobjects.
    I've tried many ways to implement what I needed like activate the whole object or the certain script attached to the object even disabling the collider.
    I thought it might be owing to the editor bug so I ran the game on Android device and the same exact thing happened.
    My laptop is a high-end one and if it has anything to do with the performance stuff it can't happen on laptop yet it happens.

    initially the trigger activates 2 empty game objects and each one has a scripts attached to them here is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TriggeringLevel : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private GameObject GameManager;
    9.     [SerializeField]
    10.     private float startingTime;
    11.     [SerializeField]
    12.     private GameObject spawners;
    13.    
    14.     public AudioCore audioCore;
    15.     private BoxCollider2D edge;
    16.     private JungleOneCore core;
    17.  
    18.     private int charge = 0;
    19.    
    20.  
    21.     private void Start()
    22.     {
    23.         //edge = GetComponent<BoxCollider2D>();
    24.         core = GameManager.GetComponent<JungleOneCore>();
    25.     }
    26.  
    27.     void OnTriggerEnter2D(Collider2D collision)
    28.     {
    29.         if (collision.gameObject.tag == "Player" && charge < 1)
    30.         {
    31.             //edge.enabled = false;
    32.             spawners.SetActive(true);
    33.             core.enabled = true;
    34.             audioCore.choosingBattleSound();
    35.             charge++;
    36.         }
    37.     }
    38.  
    39.    
    40. }
    I've searched unity forums and discussion yet every similar problem remained unanswered.
    I appreciate your helps in advance.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    It's only four or five lines of code, try commenting each one out, figure out what's chonking.

    I'm guessing it's the audioCore call.
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    plmahdi likes this.
  4. plmahdi

    plmahdi

    Joined:
    May 6, 2017
    Posts:
    2
    It works out ty!
    For those who might have the same problem in their games:
    After posting this issue on unity forum I thought the problem was my code so I've tried different ways to implement sounds in my game yet the lag remains unchanged, by checking the url you've sent I found out the problem was the wav file import settings.
     
    Chubzdoomer likes this.