• 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 thegodlygod · May 22, 2015 at 02:24 PM · clones

Instantiated clones of enemy prefab cannot be attacked?

I've set up an enemy manager to spawn clones of my enemy prefab but the spawned clones can't seem to be attacked. Here's the enemy spawn script:

 public class EnemyManager : MonoBehaviour
 {
     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
     public GameObject enemy;                // The enemy prefab to be spawned.
     public float spawnTime = 3f;            // How long between each spawn.
     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
 
     void Start ()
     {
         InvokeRepeating ("Spawn", spawnTime, spawnTime);
     }
     
     
     void Spawn ()
     {
         if(playerHealth.currentHealth <= 0f)
         {
             return;
         }
         
         int spawnPointIndex = Random.Range (0, spawnPoints.Length);
         
         Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
     }
 }

and here's the player attack script:

 public class PlayerHit : MonoBehaviour {
     public int damagePerHit = 20;                  
     private float timeBetweenPunches = 0.15f;        
 
     float timer;                                    
     GameObject enemy;                          
     EnemyHealth enemyHealth;                    
     bool enemyInRange;
     Animator anim;
 
     void Awake () {
         enemy = GameObject.FindGameObjectWithTag ("Enemy");
         enemyHealth = enemy.GetComponent <EnemyHealth> ();
         anim = GetComponent <Animator> ();
     }
 
     void Update() {
         timer += Time.deltaTime;
         if (Input.GetKeyDown (KeyCode.F) && timer >= timeBetweenPunches) {
                         anim.Play ("punch");
                         Hit ();
                 }
         if (Input.GetKeyDown (KeyCode.G) && timer >= timeBetweenPunches) {
                         anim.Play ("hpunch");
                         Hit2 ();
                 }
         }
 
     void OnTriggerEnter (Collider other)
     {
         if (other.gameObject == enemy) {
                 enemyInRange = true;
             }
         }
 
     void OnTriggerExit (Collider other)
     {
     if (other.gameObject == enemy) {
                 enemyInRange = false;
             }
         }
 
     void Hit ()
     {
         timer = 0f;
             
             // If the EnemyHealth component exist...
         if(enemyHealth.currentHealth > 0 && enemyInRange)
         {
             // ... the enemy should take damage.
             enemyHealth.TakeDamage (damagePerHit);
     }
     }
         void Hit2 ()
         {
         timer = 0f;
             
         // If the EnemyHealth component exist...
         if (enemyHealth.currentHealth > 0 && enemyInRange) {
             // ... the enemy should take damage.
             enemyHealth.TakeDamage (30);
         }
     }
 }
 
 
 



Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · May 22, 2015 at 02:28 PM

You need to work around the issue that when you clone(what i assume is prefab named "Enemy") that the gameobject's name changes to something like "Enemy(Clone)". In your attack script you make an assumption that the clone will always be named "Enemy" and that named "Enemy" is probably not in the same by that name.

You can explicitly change the name by getting the reference to the GameObject when you instantiate it.

Change:

  Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);

To:

 GameObject newEnemy = (GameObject)Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
 newEnemy.name = "Enemy";

this will only work if you have one, otherwise it will always return the first named "Enemy" object if there are multiple on the scene/screen.

Comment
Add comment · Show 1 · 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
avatar image thegodlygod · May 25, 2015 at 02:17 AM 0
Share

I tried your fix and I'm still unable to attack the instantiated enemy clones, although the instantiated clones did get their names changed to just "Enemy", so that's probably not the issue. Do you have other suggestions on how to fix this?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Destroy multiple instances of an object 1 Answer

How do I get transform.localScale (C#) to apply to a clone generated by an object rather than the object the script is attached to? 1 Answer

i have too many bullets... 2 Answers

How to change the velocity of a clone? 1 Answer

deleting only cloned objects 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