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 netherboss123 · Feb 19, 2015 at 05:52 AM · script.fpsjavatags

finding a tag with a variable

hello. i downloaded this Ai from a video (see code here). on line 2 and 3, i tried doing this code:

 var Target : GameObject[];
 Target = GameObject.FindGameObjectsWithTag("BlueTeam");

so it could target any game object with the BlueTeam tag but it didnt work. please could somebody help me fix this?

Comment
Add comment · Show 9
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 netherboss123 · Feb 18, 2015 at 12:23 PM 0
Share

PS. by "didnt work" i mean that nothing happens and the enemy does not target me

avatar image HarshadK · Feb 18, 2015 at 01:20 PM 0
Share

Do your gameobject to target have tag BlueTeam to it?

avatar image netherboss123 · Feb 18, 2015 at 03:28 PM 0
Share

yes it does

avatar image netherboss123 · Feb 19, 2015 at 03:20 AM 0
Share

Tried some other stuff and it doesn't work either

avatar image HarshadK · Feb 19, 2015 at 05:40 AM 1
Share

Put your line:

 Target = GameObject.FindGameObjectsWithTag("BlueTeam");

inside the Start() function;

Show more comments

1 Reply

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

Answer by DRRosen3 · Feb 19, 2015 at 08:06 PM

 Target = GameObject.FindGameObjectsWithTag("BlueTeam");

...should be...

 Target = FindGameObjectsWithTag("BlueTeam");

Comment
Add comment · Show 5 · 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 netherboss123 · Feb 22, 2015 at 07:11 PM 0
Share

now it finds the object but doesnt follow or attack it

avatar image The Red Mohawk · Feb 22, 2015 at 08:13 PM 0
Share

Not entirely sure and javascript isn't my strong suit, but it looks to me like even though Target is an array of multiple objects, the code you posted treats it as a single gameobject.

In function lookAt ():

 var rotation = Quaternion.LookRotation(Target.position - transform.position);

What if there are 1000 Targets? How is your object supposed to rotate?

When I operate with GameObject[] arrays in c#, I usually handle them with "foreach".

avatar image DRRosen3 · Feb 22, 2015 at 10:08 PM 0
Share

You need an additional variable for a specific target. Here's some code that I use.

 public GameObject target;
 public List<GameObject> targets;
 
 private void AddAllTargets()
     {
         GameObject[] go = GameObject.FindGameObjectsWithTag("BlueTeam");
         
         foreach(GameObject g in go)
         {
             AddTarget(g);
         }
     }
 private void AddTarget(GameObject target)
     {
         if(target.gameObject != this.gameObject)
             targets.Add(target);
     }
 private void SortTargetsByDistance()
     {
         targets.Sort(delegate(GameObject c1, GameObject c2){
             return Vector3.Distance(this.transform.position, c1.transform.position).CompareTo
                 ((Vector3.Distance(this.transform.position, c2.transform.position)));   
         });
     }
 private void TargetEnemy()
     {
         if(target == null)
         {
             SortTargetsByDistance();
             target = targets[0];
             hud.SetTarget(target.GetComponent<Enemy>());
         }
         else
         {
             int index = targets.IndexOf(target);
             
             if(index < targets.Count - 1)
             {
                 index++;
             }
             else
             {
                 index = 0;
             }
             target = targets[index];
         }
     }
avatar image netherboss123 · Feb 22, 2015 at 10:11 PM 0
Share

So... do I add this to the ai code or is it a separate code

avatar image DRRosen3 · Feb 22, 2015 at 10:27 PM 0
Share

Well this code (for me) is strictly for player input. I have it set up so that at the start AddAllTargets is called. void Start() { AddAllTargets(); } Then when player presses a certain key (for example TAB) the list of targets is sorted by their distance from the player, and the closest target is selected. I'm not 100% sure how you'll need to implement this. I just wanted to point/guide you in the right direction.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

is same code written in less lines, more beneficial ? 3 Answers

I need help with Javascript 2 Answers

Tag detection not working with rigidbody 0 Answers

Can you make an online FPS fully with Java? 2 Answers

Trying to deactivate a script when I'm not in the collider area 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