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 Mrolas · Feb 16, 2012 at 02:26 AM · raycastarraywaypointrangeindex

Array index is out of range and Raycast question

I'm trying to make a tower defense game and right now I am working on the simple AI for the enemy.

I want the enemy to, when created, make a ray to each of the waypoints, in some order. And if there is something between the enemy and the waypoint, it will move on and check the next waypoint.

Here is the code I have right now. It is telling me I have an error on line 25, which is in the start function. It is also not going through with the raycast, or if it is, then it is coming up with any results. Meaning, it is neither giving me a message, nor changing the current waypoint.

 var speed:                                float;
 var waypointLength:                        int;
 var myPos:                                Vector3;
 var hit:                                RaycastHit;
 var spawnPoint:                            GameObject;
 
 private var waypointObjects:            GameObject[];
 private var waypoint:                    Transform[];
 private var curWaypoint:                int;
 
 waypointObjects =                         GameObject.FindGameObjectsWithTag("Waypoint");
 waypointLength =                         waypointObjects.length;
 
 speed =                                 2;
 spawnPoint =                             GameObject.FindGameObjectWithTag("Spawn");
 
 //    -    -    -    -    -    -    -    -    -    -
 
 function Start () {
     
     waypoint = new Transform[waypointLength];
     waypoint[0] = spawnPoint.transform;
     
     for (i = 1; i<waypointObjects.length+1; i++) {
         waypoint[i] = waypointObjects[i].transform;
     }
     
     curWaypoint = 0;
     
 }
 
 //    -    -    -    -    -    -    -    -    -    -
 
 function Update () {
     
     transform.rotation = Quaternion.identity;
     myPos = transform.position;
     
     if (curWaypoint < waypoint.length) {
     
         var target : Vector3 = waypoint[curWaypoint].position;
         var moveDirection : Vector3 = target - transform.position;
         var velocity = rigidbody.velocity;
     
         if (moveDirection.magnitude < 0.5) {
             
             for (i=0;i<waypoint.length;i++) {
                 
                 Debug.Log("Before For");
                 
                 if (Physics.Raycast(myPos,myPos-waypoint[i].position,hit,Vector3.Distance(myPos,waypoint[i].position))) {
                     if (hit.collider.gameObject.tag == "Wall") {
                         Debug.Log("Wall hit");
                     } else {
                         curWaypoint = i;
                     }
                 }
             }
             
         } else {
             velocity = moveDirection.normalized * speed;
         }
     }
     
     rigidbody.velocity = velocity;
     
     Debug.DrawLine(transform.position,waypoint[curWaypoint].position,Color.green);
 }
 
 //    -    -    -    -    -    -    -    -    -    -

Also, if anyone could tell me, or at least point me in the right direction, how to make it so it does not go back to any waypoints it has already been to.

Thank you for your help in advance.

Comment
Add comment · Show 3
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 syclamoth · Feb 16, 2012 at 02:27 AM 0
Share

If your array has zero elements, array[0] (which, of course, refers to the first element in the array) will be out of range. You'll need to first make sure that the array has elements in it.

Also, you are using 'GameObject.FindWithTag' as a field initialiser. This is dangerous. You should do that in Start, ins$$anonymous$$d.

avatar image Mrolas · Feb 16, 2012 at 01:31 PM 0
Share

Thanks for the help. But, my array doesn't have zero elements. At least, I'm pretty sure it doesn't, because in the line above the one where I set the first term, I set the array's length to that of another array's.

avatar image FishBone · Feb 16, 2012 at 01:35 PM 0
Share

"waypointLength = waypointObjects.length;" should also be moved to the Start-function.

0 Replies

· Add your reply
  • Sort: 

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Arrays rebels to my power! 1 Answer

OnGui function - Array is out of index question 1 Answer

Array Index out of range? 1 Answer

Index out of range exception - C# - Can't find the problem 1 Answer

Array index is out of Range!? 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