- Home /
How I can reuse a bullet prefab whith enemies and my character?
I'm new in Unity and I try to make a simple game.
I have prefab enemies, my prefab character and a prefab bullet with "Bullet" tag. When my character shot the bullet and impact an enemy, the bullet and the enemy are destroyed, The problem is when the enemy try to shot a bullet, my code interacts with the enemy and the bullet and the enemy are destroyed when the enemy shot the bullet.
I know the problem and I've thought create two prefab bullet, one for my character and other for the enemy. But, are there a better form to do that??
Thanks, here my code.
oncolision fuction on lifeEnemy.js
function OnCollisionEnter(collision: Collision) {
if (collision.gameObject.tag=="Bullet"){ num_life--; }
if (num_life <= 0) { Destroy(gameObject); }
Destroy(collision.gameObject); }
update function on shotEnemy.js
var v = Vector3(transform.position.x, 0, transform.position.z);
var instantiatedProjectile : Rigidbody = Instantiate(bullet, v, Quaternion.identity); instantiatedProjectile.velocity = transform.TransformDirection(Vector3(0, 0, -bullet_speed)); Physics.IgnoreCollision(instantiatedProjectile.col lider, transform.root.collider);
Answer by fafase · Aug 05, 2012 at 10:08 AM
You would have to create a second prefab of the bullet with a different tag.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075216im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate a Prefab thats been passed through 2 scripts? 2 Answers
Script is working incorrectly. (rotation) 1 Answer
Monster Spawner 3 Answers
Set a Prefab's variable when you instantiate it, but before it calls Awake? 2 Answers
Prefab Spawning 2 Answers