• 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 ocularcash · Nov 15, 2011 at 08:43 AM · arrays

targeting multiple enemies issue

For some reason I'm getting an error with this code. What am I doing wrong? I want it to engage at any target with the same tag but I keep getting the same error on the var targetDir "Not set to an instance" I believe is what it says.

 var target : GameObject[];
 var Range = 4;
 
 function Update()
 {
     target = GameObject.FindFindGameObjectsWithTag("Enemy");
     var forward = transform.forward;
     var targetDir = target.position - transform.position;
     var angle = Vector3.Angle(targetDir, forward);
     if(Vector3.Distance(transform.position, target.position) < Range)
     {
         if(angle < 45)
         {
             Engage();
         }
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

2 Replies

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

Answer by chemicalvamp · Nov 15, 2011 at 09:09 AM

Well you see target is an array, just saying target.position isn't going to have a value. But you do target[0].position will give you the position of the first gameobject in that array. You could check each of them by doing something like this:

 var objectArray : GameObject[];
 var Range = 4;
 
     function Update()
     {
         objectArray = GameObject.FindFindGameObjectsWithTag("Enemy");
         for (int i = 0; i < objectArray.Length; i++)
         {
             var target = objectArray[i];
             var targetDir = target.position - transform.position;
             var angle = Vector3.Angle(targetDir, forward);
             if (Vector3.Distance(transform.position, target.position) < Range)
             {
                 if (angle < 45)
                 {
                     Engage(target);
                 }
             }
         }
     }
Comment
Add comment · Show 6 · 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 ocularcash · Nov 15, 2011 at 10:11 AM 0
Share

On yours when I add the forward and delete one the of the Find's from FindGameObjects I'm getting System.Int32.position not found

avatar image chemicalvamp · Nov 15, 2011 at 10:13 AM 0
Share

Sorry, see if "var target = objectArray[i].gameObject;" works.

avatar image ocularcash · Nov 15, 2011 at 10:33 AM 0
Share

Now I'm getting the same error as before

avatar image chemicalvamp · Nov 15, 2011 at 07:37 PM 0
Share

The next thing I would try is replacing target.position to target.transform.position. Also if you haven't already Engage needs to have an input parameter to receive the target. I did something kinda similar to this for one of my games, but ins$$anonymous$$d of searching through all objects with that tag, I used a custom mesh for collision kinda like FOV and then did a raycast to test line of sight as well as spheres to catch sounds (Sorry I cant test this myself and give you a definitive answer I just reinstalled windows and don't have anything installed yet.)

avatar image ocularcash · Nov 16, 2011 at 09:47 PM 0
Share

Thanks :) that works perfectly just adding transform. Def props on that

Show more comments
avatar image
0

Answer by syclamoth · Nov 15, 2011 at 08:50 AM

target is an array of gameObjects- it doesn't have a 'position' member variable! I think for this to make sense, you need to loop over all the members of 'target'. Do you want to call the 'Engage()' method for every single enemy in range, or only for one? In any case, the code is not much different.

for(var curTarg : GameObject in target) { var targetDir : Vector3 = curTarg.transform.position - transform.position; if(Vector3.Distance(curTarg.transform.position, transform.position) < range && Vector3.Angle(targetDir, transform.forward) < 45) { Engage();

     // if you want to call Engage for every singe candidate
     // (rather than if there is only one), comment this out.
     break;
 }


}

Comment
Add comment · Show 3 · 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 ocularcash · Nov 15, 2011 at 10:07 AM 0
Share

I get the error Operator < cannot be used with a left hand side of type 'float'

avatar image syclamoth · Nov 15, 2011 at 10:15 AM 1
Share

Sorry, did you just copypasta that? I don't think I spelled 'range' the same way you did, since I object to capitalised variable names.

avatar image ocularcash · Nov 15, 2011 at 10:23 AM 0
Share

ya. I capitalized it but I'm still pulling errors. I tried a few different variations but I just can't get it. I don't have any troubles adding arrays right when I'm just using a radius but for using angle it seems like the errors just don't go away

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Good Setup for Predefined Array Data? 0 Answers

going through each item of a multi-dimensional array 1 Answer

What is the best way to clear an array of Components? 2 Answers

hi!does any know how to make an endless track 1 Answer

Get ParticleSystem (Shuriken) to play from array of game objects C# 2 Answers


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