• 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 (assuming 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Object reference not set to an instance of an object 3 Answers

How to create an array of Game Objects with different tags. 1 Answer

Distance thing with gameobject ? 4 Answers

Add GameObject to GameObjectArray[] 3 Answers

I found a way to LINK JS AND C# but i have 1 problem 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges