Object Pooling not good enough...

I have tried Lists, Dictionary (to hold more values), & instantiating. all 3 cannot even handle 200 units on the screen. Any suggestions? Is the problem I’m doing this in late update in some way? How else would you do a pulse check. I’ve tried every single way I can think of, delayed late update, and trying an already running check on coroutine. Here is the way i’m currently doing it. the inUnitTrigger is from the OnTriggerEnter so it doesn’t even start pulsing until something is in range.

    bool triggerFireIsRunning = false;
    IEnumerator TriggerFire()
    {
        triggerFireIsRunning = true;
        if (curTarget != null)
        {
            FireBullet();
        }
        yield return new WaitForSeconds(delayTime);
        triggerFireIsRunning = false;
    }

    void FireBullet()
    {
        for(int i = 0; i < BulletCache.Count; i++)
        {
            if (!BulletCache[i].activeInHierarchy)
            {
                BulletCache[i].transform.position = transform.position;
                BulletCache[i].SetActive(true);
                break;
            }
        }
    }

    void LateUpdate()
    {
        if(!triggerFireIsRunning && inUnitTrigger) StartCoroutine(TriggerFire());
    }

What do you mean by ‘cannot even handle 200’? If you have Pro, have you tried the profiler? I would guess, given the limited information, is that the for loop is what’s taking up the most time. Have you tried having one list for inactive bullets and one for active and moving them between lists as needed? Then you would only have to pull the first bullet in the inactive list taking the slow for loop away.

hmm that might be JUST the solution i was looking for! However most of the freeze is when the 200 units first come in contact with the other 200 is ontriggerenter bad for handling such a massive amount of units?

i like the idea but how would you get away with this without a single for or foreach, you still need to repeat until the object the bullet is going to is destroyed or disabled i prepped like so:

void FireBullet(int index)
    {
        NotFiredBullets[index].SetActive(true);
        FiredBullets.Add(NotFiredBullets[index]);
        NotFiredBullets.Remove(NotFiredBullets[index]);
    }

    void ResetBullets(int index)
    {
        FiredBullets[index].SetActive(false);
        NotFiredBullets.Add(FiredBullets[index]);
        FiredBullets.Remove(FiredBullets[index]);
    }

ok after profiling it looks like around 70% of my load is from this script so i am in the right script but…
200units checking 200units with 3 lists is an issue…i guess the 2 bullet ones are not gonna be that big of a deal since they are only 3 max half of the problem is with enter half of it is with exit somehow…hmm…possible the adding/removing of playerunit contently? It has been extremely difficult to optimize this for me for some reason :frowning:

public Transform curTarget;
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.layer == 10) //12 = Player Infantry
        {
            FindTarget(other.transform);
            inUnitTrigger = true;
            PlayerUnit.Add(other.transform);
            // player needs to fire
        }
    }
 
    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.layer == 10)
        {
            inUnitTrigger = false;
            PlayerUnit.Remove(other.transform);
         
            if (PlayerUnit.Count >= 1)
                curTarget = PlayerUnit[0];
            else
                curTarget = null;
        }
    }
 
    void FindTarget(Transform other)
    {
        if (curTarget != other)
        {
            if (FindCurrentClosest(other, curTarget))
                curTarget = other;
        }
    }
 
    bool FindCurrentClosest(Transform target, Transform curtarget)
    {
        if (curTarget != null)
        {
            float newtarget = (target.position-transform.position).sqrMagnitude;
            float currenttarget = (curtarget.position-transform.position).sqrMagnitude;
         
            if (newtarget < currenttarget)
                return true;
            else
                return false;
        }
        else
            return true;
    }

I take it you using a large sphere collider to see who is in shooting range?

If it isn’t likely that player’s won’t be all grouped together, then I would try a second sphere collider with a shorter range and then, if that list has anything in it you would know it is closer than the outer sphere.

Another approach is to have the player’s scripts themselves update the ‘zone’ they are in every once in a while in the Update() call. By zone, I mean section the play area into grid squares and have a list for each. Then you would check progressively further zones.

I kind of did what you said tango. Made 1 global sensor per enemy and then the basic one for attacking so now AI is a breeze and i underestimated physics.sphereoverlap it’s better than expected when using a layermask!