• 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 davidflynn2 · Aug 21, 2013 at 03:40 PM · c#errorai

Argument out of range.

I am using the following script to create an Enemy Ai the issue I have hit is if there is no target it gives me the Argument out of range. If I make it add its self to the array when there is no target the ai just starts to fly forward nonstop.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic; 
 
 public class EnemyAi : MonoBehaviour 
 {
     //public Transform target;//Targets.
     public int moveSpeed;//Speed enemy can move.
     public int rotationSpeed;//Speed enemy can rotate.
     public int detectionDistance;//The distance the enemy can detect you from.
     public List<Transform> targets = new List<Transform>();
     
     
     
     private Transform myTransform;//Save for the transform.
     
     void Awake()
     {
         myTransform = transform;//This saves our transform so we dont have to look it up all the time.    
         //targets.Add(myTransform);
     }
     
     
     void Update ()
     {
     
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(targets[0].position - myTransform.position), rotationSpeed * Time.deltaTime);//Makes it look at us using the speed we set over time.
 
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
         
     }
     
      void OnTriggerEnter(Collider other) //When collided
     {
         if(other.tag == "Player")
         {
             targets.Add(other.transform);
             //targets.Remove(myTransform);
         }
         
         
     }
     void OnTriggerExit(Collider other) //When collided
     {
         if(other.tag == "Player")
         {
             targets.Remove(other.transform);
             //targets.Add(myTransform);
         }
         
         
     }
 }
 
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

1 Reply

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

Answer by whydoidoit · Aug 21, 2013 at 03:46 PM

At the start of Update check if the count is 0 and return if it is:

  void Update()
   {
        if(targets.Count < 1) return;

         ...
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 davidflynn2 · Aug 21, 2013 at 03:51 PM 0
Share

Ok so that sets the targets back to 0 again I see. Thanks for your help.

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

16 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

Related Questions

Multiple Cars not working 1 Answer

error cs0236 field initializer cannot reference a nonestatic field, method or property 1 Answer

BurgZergArcade tutorial 20 errors? 0 Answers

Can't draw a box using Basic GUI 1 Answer

NullReferenceException problem 2 Answers

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