I have a singleton class tied to an object. This has an array of enemies in it. When I instantiate an enemy (1 enemy every 2 seconds for 10 seconds total) I add it to this list.
I don’t want these enemies to collide with each other. So after one is created, I look through the enemy list to run the Physics.IgnoreCollision() between the new one and the ones in the list. I then finish by adding the new one in the list.
So basically:
for(var e : GameObject in SceneGameState.instance.enemies)
{
Physics.IgnoreCollision(e.GetComponent(Collider), z.GetComponent(Collider));
}
// Add this enemy to the global list
SceneGameState.instance.enemies.Add(z);
The for loop look is causing just a TON of enemies to be spawned!! If I take the for loop out it works as normal with 1 enemy ever 2 seconds for 10 seconds. Whey would this for loop cause way more enemies to spawn? It should just be looping through the list of enemies and making their character controllers not collide with each other.
I’m not home right now, but yeah it doesn’t make sense how the for loop would effect this. So I have a wave manager object (think tower defense wave of enemies) where I attach wave objects to. The wave objects have spawner objects attached to it. The spawner objects have specific enemy factory objects attached to it that do the actual spawning of that specific enemy. So the factory object is the only thing calling Instantiate(). The Spawner object controls the duration, frequency, and count of when and how many enemies spawn. So somehow that for loop is making the Spawner object speed up the production of enemies, which doesn’t seem to make any sense.
There are many scripts that work together but I’ll post them when I get home.
You don’t have to use the Ignore.Collision function. All you have to do is use the gameObject you want to spawn as a prefab, then on the collider for the gameObject set the collision layer to ignore the collisions for all things w/ the tag, oh, say, “Friendly.” Just make sure you update the prefab every time you make any changes.
Oh nice, I didn’t know that. Collision layers are new to me. The other engine I had didn’t have layers exactly. It allowed you to setup integers for collision groups then define which groups collide with each other. I’ll take a look at this tonight. Thanks!
Hey, I’ve only really looked into unity for a day now so hopefully i remembered this correctly.
In the inspector window, right under the GameObject’s name, there should be a layer drop down box. You should be able to create your own layer and set the GameObject’s layer from there. There was a tutorial on layers… but can’t seem to remember which one exactly :(. I think i had a hard time trying to editing a free user layer (couldn’t right click to rename or edit it in any way) and yet to figure out how.