- Home /
copy enemy prefabs
I created an Enemy with various components and 1 children i make it a prefab, and a want to use it several times around the scene, but when there are more than 1, only 1 work correctly. this script its attached to the prefab.
public class fireBullet : MonoBehaviour {
GameObject Fireposition;
GameObject enemyBody;
public Transform rager;
Transform enemy;
public PoderControl poderControl;
float distance;
float weaponEnemy;
float AddForceX,AddForceY;
public int enemyLife;
void Start () {
Fireposition = GameObject.FindGameObjectWithTag("fire");
enemy = GetComponent<Transform> ();
enemyBody = GameObject.FindGameObjectWithTag("enemy");
AddForceX = 0f;
AddForceY = 0f;
enemyLife = 300;
}
void FixedUpdate()
{
MoveEnemy ();
ChequearPosicionRager ();
StartCoroutine (ShootBullet (AddForceX, AddForceY));
DestroyEnemy ();
}
IEnumerator ShootBullet(float AddForceX,float AddForceY)
{
distance = Vector2.Distance (rager.transform.position, Fireposition.transform.position);
if (distance < 12f) {
if (Time.time > weaponEnemy + 0.5f) {
weaponEnemy = Time.time;
GameObject bulletinstance;
bulletinstance = Instantiate (Resources.Load("Bullet"), Fireposition.transform.position, Fireposition.transform.rotation) as GameObject;
bulletinstance.rigidbody2D.AddForce (new Vector2 (AddForceX, AddForceY));
Destroy (bulletinstance, 1f);
}
}
yield return new WaitForSeconds (3f);
}
void MoveEnemy()
{
Vector3 dir = rager.transform.position - enemy.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
enemy.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
void ChequearPosicionRager()
{
if (rager.position.x < enemy.position.x && rager.position.y < enemy.position.y) {
AddForceX = -100f;
AddForceY = -50f;
}
if (rager.position.x < enemy.position.x && rager.position.y > enemy.position.y) {
AddForceX = -100f;
AddForceY = 50f;
}
if (rager.position.x > enemy.position.x && rager.position.y > enemy.position.y) {
AddForceX = 100f;
AddForceY = 50f;
}
if (rager.position.x > enemy.position.x && rager.position.y < enemy.position.y) {
AddForceX = 100f;
AddForceY = -50f;
}
}
void OnTriggerExit2D(Collider2D poder)
{
if (poder.gameObject.tag == "poder")
enemyLife -= 25;
}
void DestroyEnemy()
{
if (enemyLife<=0)
enemyBody.SetActive (false);
}
}
So it is an AI spcript for enemy, right? Why do you storing 2 variables: enemyBody and enemy? They storing gameObject and transform of the enemy? If yes when you use
enemyBody = GameObject.FindGameObjectWithTag("enemy");
in line 17 enemybody references to only one of gameObject with tag "enemy". If I think right you need this:
enemy = transform;
enemyBody = gameObject;
FindGameObjectWithTag() is good when you need to do something with all objects with same tag. In this case it should be placed in cycle. Other way to use this func is in collision detection. But in your case it's wrong to use it, and it is right only when there is only one object with some tag. Hope this info will help you:)
Fireposition = GameObject.FindGameObjectWithTag("fire");
And what is fireposition? Is it a single object or it is one of the Enemy's children?
yes, that was an improveand a fix, but the problem is that only 1 prefab shoot the bullet while the other only moves,
its Enemy`s children, its any other function to use in this case?. Thanks
Answer by ReContraChanfle · Mar 13, 2014 at 07:05 AM
Problem Fixed replacing this:
Fireposition = GameObject.FindGameObjectWithTag("fire");
enemy = GetComponent<Transform> ();
enemyBody = GameObject.FindGameObjectWithTag("enemy");
by this:
enemy = transform;
enemyBody = gameObject;
Fireposition = enemyBody.transform.FindChild ("fire").GetComponent<Transform> ();
i was accesing the FirePosition through tag searching an that was making and erratic behaviour in the prefab,
Your answer
Follow this Question
Related Questions
gameObject are not referenced 2 Answers
[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers
Trouble with destroying an instantiated prefab 2 Answers
Assign an instantiated GameObject? 1 Answer
Instantiate A Prefab 1 Answer