Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by deglet · May 01, 2016 at 04:39 PM · prefabpositionenemy

Get position of prefab enemy ?

Hello,

I have a prefab of enemies who spawn regularly. I try to get the position of them in order to kill them. It work with the one already instanciate but not the new.

First I get the gameobject and the position from the enemy script:

public GameObject Foe = GameObject.Find("o_foe"); public float PosXFoe; public float PosZFoe;

Then:

void Start() {

     Foe = GameObject.FindGameObjectWithTag("Foe");
     FoeHealth = Foe.GetComponent<s_EnnemyControl>();

}

void Update() {

      PosXFoe = GameObject.Find("o_foe").GetComponent<s_EnnemyControl>().PosXFoe - 2;

     PosZFoe = GameObject.Find("o_foe").GetComponent<s_EnnemyControl>().PosZFoe - 2;

}

And the attack:

void GestureAttack()

    {
     if (recupgeste == "D" && recupgestescore > 0.50)
     {
       
         player.transform.position = new Vector3(PosXFoe, 1, PosZFoe);

         if (FoeHealth.currentHealth > 0 /*attackphas*/)
         {
             // ... damage the player.
             FoeHealth.TakeDamage(attackDamage);
         }

     }
 }

Help me, thanks.

Comment
Add comment · Show 4
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 Cherno · May 01, 2016 at 07:08 PM 1
Share

Avoid using GameObject.Find. Ins$$anonymous$$d, keep a list of instantiated enemy gameobjects, and add or remove from it when you instantiate and destroy them. This way, you always have a reference to each one that's currently in the scene.

Also note that if you instantiate a clone of a prefab, " Clone(0)" or something along that line will be added to the clone's name, so GameObject.Find won't help you in those cases if you don't manually change the clone's name back to the prefab's name.

avatar image tuncOfGrayLake Cherno · May 01, 2016 at 08:30 PM 0
Share

Cherno has it right.

You can use a List for keeping track of all your creations or simply a variable to keep reference to the last enemy created.

With a list you can do the following:

 import System.Collections.Generic; //Helps you use List
 public List enemies;
 public GameObject lastEnemySpawned;
 public GameObject enemyPrefab; //In inspector drag and drop your enemy prefab onto this. 
 
 public class SomeClass
 {
     //Create your list on awake or start
     void Awake()
     {
     enemies = new List<GameObject>();
     }
         
     //Spawn your enemy and keep a reference for it, then add it to the list of enemies.
     private void SpawnEnemy()
     {
     lastEnemySpawned = (GameObject) Instantiate(myEnemyPrefab, transform.position, transform.rotation);
     enemies.Add(lastEnemySpawned);
     }
 }


avatar image Cherno tuncOfGrayLake · May 01, 2016 at 09:04 PM 1
Share

I'd like to add that it's necessary to import System.Collections.Generic in order to use Generic Lists.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObject position and localPosition not changing in hiearchy, only in script. 0 Answers

How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer

Changing position of instantiated prefab from empty gameobject which is the location of the prefab when instantiating. 0 Answers

Setting the transform.position not working 1 Answer

Lerp to position in instantiated 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