- Home /
Error: Instantiated Enemies don't get hit
So im spawning enemy prefabs in random locations (donut spawn) around the player.
When the player shoots the enemy, the bullets go right through them. The collision doesnt occur.
I did:
GameObject enemy = (GameObject) Instantiate(Resources.Load("Enemy"), SpawnVector(5,15), Quaternion.identity);
It works fine when I drop the enemy in through the editor and run the game.
Please Help!
I couldn't find a solution using search.
Well, would be better to help you with the enemy script...try to review the collision system into the enemy prefab
The enemy has a character controller attached to it and doesnt have a collision method in the enemy script.
In the bullet script i have:
void OnCollisionEnter(Collision Other)
{
if ( Other.gameObject.GetComponent( typeof(AIscript) ) != null && Other.gameObject != objPlayer )
{
AIscript enemy = (AIscript) Other.gameObject.GetComponent( typeof(AIscript) );
enemy.health -= 10;
remove$$anonymous$$e(); //destroys bullet
}
}
Not sure why dropping an enemy in works. Typically Character Controllers are attached to objects without colliders and don't get or generate OnCollisionEnter().
Firstly, have you tried putting a Debug.Log(); in OnCollisionEnter to see if it gets called? If you have and it does not fire, have you tried to use OnTriggerEnter ins$$anonymous$$d of OnCollisionEnter?
$$anonymous$$aybe ins$$anonymous$$d of OnCollisionEnter try using OnControllerColliderHit in the enemy.
Answer by DDP · Sep 30, 2013 at 05:17 PM
Try adding a rigidbody on your bullet. If your bullet is a trigger, you may need to check the "is kinematic" box.
Answer by HendrysTobar · Sep 30, 2013 at 05:29 PM
Try adding Rigidbodies to both, the bullet prefab and the enemy prefab.