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 07Mr07 · Jan 05, 2014 at 04:17 PM · gameobjectfindwithtag

How change target with GameObject.FindWithTag

I have a turret and should do:

-when see an enemy shoot, when he is out of range or dead(destroyed), turret must target next enemy.

But my script is doing:

-target first enemy and only target rest when first dead

 function Update()
 {
 
 target = GameObject.FindWithTag("Enemy").transform;
 
      if(target && range)
      {
 
           var rotate = Quaternion.LookRotation(Objetivo.position - transform.position); 
           transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
  
 
           if (nextShotTime <= Time.time)
           {
 
               Shoot();
               nextShotTime = Time.time + timeBetweenShots;
           }
      }
 }


I need turret target next enemy when 1st out of range

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by craigaw · Jan 06, 2014 at 07:14 AM

The FindWithTag command will only return the first object that Unity finds with that tag. It doesn't take into account distance from the origin or any other object.

If you have to search by tags then use FindGameObjectsWithTag, this will return an array of objects, then simply add a check to find the first of those objects that is in range, or sort them by range first and pick the closest.

     var objs : GameObject[] = GameObject.FindGameObjectsWithTag("Respawn"); 
     for (var obj : GameObject in objs) {
         if (range) {
             target = obj.transform;
             break;
         }    
     }

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
avatar image
0

Answer by 07Mr07 · Jan 06, 2014 at 12:12 PM

Im trying this, but 'objs' is always empty :/

 function Update()
 {
     var objs : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
     
     //Debug.Log(objs);
     
     for(var obj : GameObject in objs)
     {
         distance = (obj.position - tower.position).magnitude;
         if(distance<=range)
         {
             shootRange=true;
         }
         else
         {
             shootRange= false;
 
         }
     
         if(shootRange)
         {
             target = objs.transform;
             break;
         }
     }
 }
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 craigaw · Jan 06, 2014 at 10:23 PM 0
Share

FindGameObjectsWithTag returns an array, not a single GameObject. That's why you need to use a for loop to iterate through it and the break command when you find a GameObject within range.

So when you use the Debug.Log command to view it, it will always appear empty, something like GameObject[]. It doesn't mean it's empty, it just means that the Debug.Log command won't output the contents of the array. If you use Debug.Log inside of the for loop ins$$anonymous$$d (e.g. Debug.Log(obj)), you'll see it returns something (assu$$anonymous$$g you have at least one object tagged as Enemy).

Also, your line defining the target is incorrect. At the moment, it is trying to return the transform of the array, ins$$anonymous$$d of an individual GameObject. Try using target = obj.transform; ins$$anonymous$$d.

avatar image
0

Answer by gonzalocastillocabrera · Jun 04, 2020 at 06:30 PM

From the YTChannel 'Brackeys'.

 public Transform target;
 public GameObject[] enemies;
 enemies = GameObject.FindGameObjectsWithTag("Zombie");
 float shortestDistance = Mathf.Infinity;
 GameObject nearestEnemy = null;
 foreach(GameObject obj in enemies) {
             
             distance = Vector3.Distance (obj.transform.position, this.transform.position);
             if(distance < shortestDistance){
                 shortestDistance = distance;
                 nearestEnemy = obj;
             }
 
             if(nearestEnemy != null && shortestDistance <= 9.0){
                 target= nearestEnemy.transform;
             }
             
             
         }

  

 //This is mine
 //Look Enemy
 if (target != null){
   Vector3 relativePos = objetivo.position - transform.position;
   Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
   transform.rotation = rotation;
 }
                 



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

20 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

Related Questions

"Player" tag collider from other GameObject? 1 Answer

Find the first game object created with a given name 1 Answer

JS file deleting multiple game objects 0 Answers

JavaScript 3 Arrays questions 1 Answer

What's the most efficient way of finding child objects at runtime? 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