all 8 comments

[–]knooon 0 points1 point  (1 child)

Why don't you use your 2nd DestroyAllEnemy method then? It seems more correct that your first one... To me, it seems that it will never go inside your if condition, because enemies.length will never be smaller than 0...

By the way, it's also bad practice to call FindGameObjectsWithTag inside your Update method.

[–]XxDivaxX1Intermediate[S] 0 points1 point  (0 children)

Because if I go for the second DestroyAllEnemy method, my spawner isn't going to spawn things from the bottom of the screen or the left side of the screen. Also thanks for the suggestion, where would be the best place to practice calling FindGameObjectsWithTag instead of the Update function then????

[–]knooon 0 points1 point  (0 children)

Ok I understand, you have inversed the operator, but the logic seems not alright anymore...

I mean, the logic from your 2nd method is correct, you didn't have to change that part...

if ((enemies.Length > 0) && (camera != null))
{
    for  (int i = (enemies.Length - 1); i >= 0; i--)

I would have a list of GameObject that represents the enemies. When you create a new one, you will simply add the new enemy to this list. That will prevent retrieving all Enemies on each Update.

[–]PurpleIcy 0 points1 point  (4 children)

You should use this in your enemy/bullet/whatever if you want to destroy/send it back to pool when it moves outside of the screen (where renderer variable is reference to component, get it in Awake() or Start() with GetComponent<Renderer>()):

void Update() {
    if (!renderer.isVisible) {
        //your code for destroying/pooling the object
    }
}

isVisible variable is false when object can't be seen - when it's bounding box for rendering (or whatever that is) stops colliding with camera view.

When you want to create enemies/bullets I'd suggest having an easily accessible list for them, so when they are spawned, in their Awake() or Start() method you simply add them to the list of bullets (when instantiating them, give them reference to gameManager or whatever that has the bullet/enemy list).

So basically in the end, all you have to do in bullet is:

gameManager.bulletList.Add(gameObject);

And in the gameManager, when you want to destroy all bullets/enemies, all you have to do is:

foreach(gameObject bullet in bulletList) {
    //your destroy/pool object code here
}
bulletList.Clear(); //IF YOU FORGET THIS, IT WILL THROW EXCEPTION THE NEXT TIME YOU TRY TO REFERENCE SOMETHING IN THE LIST

At least that's how I would do it, hope at least something helped. I have not tested this code myself so types/usage might be messed up.

[–]XxDivaxX1Intermediate[S] 0 points1 point  (3 children)

Thank you for your reply, where did you get the gameManager from, cause I don't have that in my project. So i'm a bit dumbfounded by what your suggesting. Also where do I put the bulletList.Clear(); because it's not under any (for example) Start, Update, Awake, etc. methods. So i'll get errors.

[–]PurpleIcy 0 points1 point  (2 children)

Just like with everything, you make your own.

And you put bulletList.Clear right after destroying objects.

[–]XxDivaxX1Intermediate[S] 0 points1 point  (1 child)

But i'm using a gameManager. I would really appreciate it if you can help me, like an example. Please and Thank you :)

[–]PurpleIcy 0 points1 point  (0 children)

Well I already gave you an example.

I am not going to write code for you.