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

First Instantiation of Particle System Lags

Discussion in 'Scripting' started by MWizard, May 17, 2019.

  1. MWizard

    MWizard

    Joined:
    Jan 5, 2017
    Posts:
    78
    I've found 3 other links to similar situations, none of which have been the answer that works.

    The first time I instantiate my super basic particle system on my Android device, it lags. Every other instantiation of the particle system is fine, but that first one causes a lag spike and the whole game locks up for a split second before moving on.

    Simply put, the user taps the object to destroy it, and it creates a particle system as feedback the object was destroyed.

    Weirdly enough, the profiler was showing it was a Render spike for the UI, but I realized that if I disabled the instantiation on the objects, and just destroyed them, then there is no spike.

    I have also tried spawning the particle system into action before the game starts, and the lag only happens when instantiated on the first destroyed object. What the heck!?

    Anyone have any ideas? Thanks in advance!
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I believe people generally work around this type of issue using Preloaded Assets in Player settings, by using an object pool, or by creating and destroying an instance of the prefab out of view of the player during a menu or loading scene.
     
    dgoyette likes this.
  3. MWizard

    MWizard

    Joined:
    Jan 5, 2017
    Posts:
    78
    So, ok. I've tried the following:
    • Preloading the offending asset
    • Instantiating the object on screen/off screen
    • Destroyed object on screen/off screen
    • Combining all of the above
    I still have a lag on the first instantiation of the particle system.

    HERE'S THE CRAZY PART! I actually broke the script and received an error and all of a sudden, the lag was no longer present. Specifically, I added a public game object to the script, wrote everything to call the instantiation, but did drag the game object from prefabs to the Inspector for the script. When I built it and ran it on the Android device, the lag was not present. If I played it in the editor, that's when I noticed that I didn't fill in that public game object. After applying the game object to the script, the lag returned.

    To give a little more information about this instantiation: There is an empty, parent game object that holds the object that gets destroyed. The instantiation is called by the parent as a coroutine due to a timer that is part of the instantiation. The child object is destroyed, the parent calls the particle system, then the parent game object destroys itself to clean up stuff.

    So, a little more specificity, I called a copy of the parent, and then called the destroy action and coroutine exactly how it is called when the screen is tapped. Given, the tap creates a ray that intersects with a "Tappable" object, and does a brief check for how many touch points there are, but I do not see this as the reason for the lag, but maybe it is.

    Long Story Short: I've done what every other thread suggests, including this response about preloading assets (never knew it was there, still new to Unity in the grand scheme of things). I've fixed the issue by literally breaking the script with an exception. Does anyone have a suggestion on how to replicate "breaking" a script to fix this without actually throwing an error? There has to be something I am missing in regards to this.
     
  4. MWizard

    MWizard

    Joined:
    Jan 5, 2017
    Posts:
    78
    UPDATE: I've found the actual culprit... it is a part in my script that uses
    GetComponent
    to set a value to the object for another portion of the GameController. I need to move that somewhere else to get set early on, but it has actually nothing to do with the Particle System. It was literally
    GetComponent
    that was the issue, and that chalks it up to overthinking things and Googling the wrong problem. Thanks for your input!
     
  5. InevitableDev

    InevitableDev

    Joined:
    Apr 6, 2021
    Posts:
    3
    Thank you! This fixed my problem. I had an unused
    Code (CSharp):
    1. GetComponent<>()
    function