Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ReContraChanfle · Mar 13, 2014 at 05:57 AM · gameobjectinstantiateprefabenemy

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);
 
     }
 
 }

Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OSG · Mar 13, 2014 at 06:21 AM 0
Share

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;
avatar image OSG · Mar 13, 2014 at 06:31 AM 0
Share

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:)

avatar image OSG · Mar 13, 2014 at 06:35 AM 0
Share
 Fireposition = GameObject.FindGameObjectWithTag("fire");

And what is fireposition? Is it a single object or it is one of the Enemy's children?

avatar image ReContraChanfle · Mar 13, 2014 at 06:36 AM 0
Share

yes, that was an improveand a fix, but the problem is that only 1 prefab shoot the bullet while the other only moves,

avatar image ReContraChanfle · Mar 13, 2014 at 06:49 AM 0
Share

its Enemy`s children, its any other function to use in this case?. Thanks

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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,

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges