Solving Performance issues

Hey i’m currently making a 2D game where a certain enemy type can roll on the ground and instantiates a prefab of oil on top of every cube making the ground then destroying the oil after 5 seconds.
The colision detection and all this instantiation makes my game lag (Didn’t built the game though so I don’t know if it gets better if I build it.) after like 15-20 oil spawned.

What is a good solution to this ? Should I Instantiate a bunch of oil prefabs outside the screen at the beginning of the game and then moving them at the correct place / Moving them offscreen instead of destroying them to reuse them ?

Also, why when my game is lagging my character jumps a lot higher than usually, even though i’m using Time.DeltaTime ?

Thanks for answering my questions have a good day.

Have you used the profiler to see what’s eating your performance?

That’s always step 1.

2 Likes

Didn’t know something like that existed, will do it right away, thanks.

So it seems that when it lags my “Oil Script” is using 90% of CPU usage.
So it’s indeed a problem of instantiation I think.

EDIT : I’ve found something called object pooling, I guess it will solve this issue.

1 Like

I made my first Coroutines today, you should look into what they can do.

1 Like

If you’re willing, post the ‘oil script’ - there may be something in there that you haven’t spotted causing extra loops/issues…

1 Like

Hey thanks for both of your answers, I’m currently looking at coroutines, quite amazing stuff !
For my issue, I’ve started using ObjectPooling I haven’t finished yet but it should solve those performance issues.
I don’t have the script that spawns oil (Because I rewrite it and It’s not even finished yet) but there was this script attached to all my spawned oil prefabs.

using UnityEngine;
using System.Collections;

public class OilScript : MonoBehaviour {

    // Make a time counter
    // Destroy  Oil after 5 seconds

    public float DespawnTimer { get; set; }

    void Start()
    {
        DespawnTimer = 0;
    }

    void Update()
    {
        Debug.Log(DespawnTimer);

        DespawnTimer += Time.deltaTime;
        if(DespawnTimer >= 5)
        {
            Destroy(gameObject);
        }
    }
}

So yeah, I think that instantiating a prefab each frame (Which was doing my other script that I don’t have anymore) and destroying each prefab after that was the thing causing trouble, I read that using Instantiate and Destroy alot at the same time was very consuming on the CPU and should be avoided (Using Pooling instead) so i’m going to go that route now.

Thanks for your help, sorry I couldn’t provide both the scripts…

Do note that doing a Debug.Log() every update is very costly… Is that causing your issue?

4 Likes

Really ? I thought it wasn’t at all, thanks for that.
Well yeah I’ve stopped the debug.log and it’s working without drops thanks.

1 Like

Debug.Log does a full stack trace. That’s why it’s so expensive. Currently Unity does not have a way to write a message to the console without a stack trace.

1 Like

Which, having never read anything about the profiler I discovered immediately unop the first time I ever saw it. Imagine what 200 NPC’s each making 4 conditional Debug.Log calls - at least two each every frame did!

The upswing is that I optimized code I didn’t need to before discovering it- and it was good practice. I was thinking 0-200 (average 100) simultaneous raycasts shouldn’t cripple a 4670k.

2 Likes