Search Unity

[SOLVED] My Game is Running Slow on Startup

Discussion in 'Scripting' started by Blue Bean Apps, Sep 2, 2014.

  1. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    I just solved a problem I've been fiddling around with for over 3 weeks and now my game starts very slow. I know exactly what the problem is, but I don't know how to fix it without having to revert my accomplishment! Is there anyway to make the game run or load smoothly? If you need to know what is going on in my script to cause this lag, I'll add the information to this post. I REALLY don't want to revert what I've been trying to do for the last 3 weeks, so I'm really hoping there is a fix to this. Is there, maybe a function or something that can allow the game to load smoothly? My game lags at the start of a level where it has to load a bunch of objects and place them randomly.

    -- Chris
     
  2. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    Use the profiler to see what is causing the most problems. Also you can black out the screen for a a few secs and add a loading bar and do everything asynchronously in a coroutine.
     
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    This is a Unity Pro feature.

    @Blue Bean Apps Could you please describe the problem?
     
  4. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    That's why I offered him two options. Coroutines can be used to place your objects asynchrously and while everything is processing you can show a loading bar rather then loading everything in one go.
     
  5. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    Yeah, I'm using the free edition, so I can't use the Profiler, but now I see how the Profiler can come in handy at times.
    What I want is the sort of thing Flappy Bird has when you die (the little menu that shows your score and allows you to play again) and while that scoreboard thing is up, reload the level behind that scoreboard.

    Any idea on how to do this?

    -- Chris
     
    Last edited: Sep 2, 2014
  6. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Do you remove obstacles as soon as they leave the camera view?
    Are you using legacy GUI or sprites acting like buttons?
     
  7. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    No. Everything in the level is loaded right from the start, which might be why it lags at the start because it's having to load a bunch of stuff at once.
    One small legacy animation (just two clips) attached to one game object that's in the scene all the time (the main character).

    In about five minutes, I will post some screenshots and info about this game in the WIP forum. Feel free to take a look!

    -- Chris
     
  8. Jacob-Williams

    Jacob-Williams

    Joined:
    Jan 30, 2009
    Posts:
    267
    NomadKing and Blue Bean Apps like this.
  9. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    Thanks, I will look into that! By the way, if you want to see more information, elmar, click the link I just added in my signature! You can see a few screenshots of my game in action right now. I will add more as the game progresses.

    -- Chris
     
  10. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Thats the issue! Make them spawn at random locations of the screen when not seen by camera. Add a trigger behind on the left side of the screen outside the camera view which then destroys obstacles.

    I am not sure, but try creating a separate game object and attach your GUI there.

    Is the character moving or you're using some sort of technique? (E.g Background is moving along with obstacles while the actual player stands still).
     
  11. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    Is there some event for something like that? Maybe something like "function onOutOfRange" or something like that, so that I can instantiate more objects when the objects are out of the camera's view range?
    The character is moving. Someone suggested to me a while back to move the background instead of the character, but at the time, I had already made a lot of the game having the character move, so I understandably didn't want to change everything around, especially if I didn't have to.

    -- Chris
     
  12. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    2D Endless runners must have this type of technique to work properly. My guess is that you put obstacles into the scene with wide background before running the game.
    The problem is that you can't physically do that.
    Look for 2D endless runner tutorial in the live archieve.

    There is function OnBecameInvisible() which detects if object is outside of camera view.

    http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnBecameInvisible.html

    I would advice you to change the approach you're making the game.
     
  13. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    Yeah, I did.
    I will give that a try.

    -- Chris
     
  14. Astroidfriog644

    Astroidfriog644

    Joined:
    Mar 25, 2022
    Posts:
    1
    I think your problem is that your script is not ..um...multitasking, which means that the spawning script is running and causing the movement script to not update. The solution is to put it in a queue. Look onyoutube.
     
  15. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Well you just replied to an 8 years old thread.