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 LivingUniverse · Mar 01, 2013 at 01:41 AM · listwaypoints

Another argument out of range (sorry)

Ok I give up, i cant figure out this argument out of range error, parameter name: index. Highlights this line of code - currentWaypoint = waypoint[randWaypoint1];

Im have a list of all the waypoints and have my enemy select 1 at random. The code is incomplete as I just started writing the class and deciding what i need to add. But at the moment in the inspector when i run it, my enemy has a list of the waypoints and in my variable "currentWaypoint" he has picked one at random 0 - 9, but some reason i get the argument out of range. This is my code -

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class WayPoints : MonoBehaviour {
     
     public List<Transform> waypoint;
 
     public float timer;
     public bool stopTimer = false;
     
     public float timeTillNextRandomWaypoint;
     public bool stopTimerRndSelect = true;
     
     public Transform currentWaypoint;
     
     public int randWaypointTimed;                        
     public int randWaypoint1;
     
     public int selectRandPause;                     
     public int selectRandChange;                    
     public float timeForPause;                        
     public bool pauseTimer = true;
     
     void Start(){
         waypoint = new List<Transform>();
         timer = 1;
         currentWaypoint = null;
     }
     
     public void AddAllWaypoints(){
               
         GameObject[] go = GameObject.FindGameObjectsWithTag("Waypoint");
         
         foreach(GameObject waypoint in go)
             AddWaypoints(waypoint.transform);
     }
     
     public void AddWaypoints(Transform waypoints)
     {    
         waypoint.Add(waypoints);
     }        
     
     public void SelectWaypoint(){
         if(currentWaypoint == null){
             randWaypoint1 = Random.Range(-1,10);
             currentWaypoint = waypoint[randWaypoint1];
             Debug.Log(randWaypoint1);
         }
 
             //  if(target is at currentwaypoint position){
     //        randWaypoint1 = Random.Range(-1, 10);
     //        currentWaypoint = waypoint[randWaypoint1];
         
     //        selectRandPause = Random.Range(0, 101);
     //        selectRandChange = Random.Range(0, 101);
         
     //        if(SelectRandPause < 30){
     //            pauseTimer = false;
     //            timeForPause = 0;
     //        }
             
 //            if(selectRandChange <= 40 && selectRandChange > 25){
 //                stopTimerRndSelect = false;
 //                timeTillNextRandomWaypoint = 6;
 //            }
 //            if(selectRandChange <= 25 && selectRandChange > 15){
 //                stopTimerRndSelect = false;
 //                timeTillNextRandomWaypoint = 4;
 //            }
         
 //          if(selectRandChange <= 15){
 //                stopTimerRndSelect = false;
 //                timeTillNextRandomWaypoint = 2;
 //            }
 //    //    }
         
     }
     
     void Update(){
         if(!stopTimer){
             if(timer > 0)
                 timer -= Time.deltaTime;
                     
             if (timer <0)
                 timer = 0;
             
                if(timer == 0){
                 AddAllWaypoints();
                   stopTimer = true; 
             }
         }
             
         if(!stopTimerRndSelect){
             if(timeTillNextRandomWaypoint > 0)
                 timeTillNextRandomWaypoint -= Time.deltaTime;
                     
             if (timeTillNextRandomWaypoint <0)
                 timeTillNextRandomWaypoint = 0;
             
                if(timeTillNextRandomWaypoint == 0){
                 randWaypointTimed = Random.Range(-1, 10);
                 currentWaypoint = waypoint[randWaypointTimed];
                   stopTimerRndSelect = true; 
             }            
         }            
         
         if(!pauseTimer){
             if(timeForPause == 0)
                 timeForPause = Time.time;
                     
             if ((Time.time - timeForPause) >= 3)
             {
                 pauseTimer = true;
             } else {
                 //pause                
             }            
         }    
         SelectWaypoint();
     }
 }
 
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
0
Best Answer

Answer by Julien-Lynge · Mar 01, 2013 at 02:08 AM

@LivingUniverse,

Please add some debug statements to your code. That will allow you to determine things like the length of the array when you're trying to access it, and what value your Random.Range returned. That will allow you to figure out your bug.

I am not going to bugfix your code for you, and many people around here will take offense at being asked to do your work for you.

You are frustrated with coding - I get that, we all get frustrated at times. However, this is your project, and it's up to you to figure out what's going wrong.

Comment
Add comment · Show 4 · 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 LivingUniverse · Mar 01, 2013 at 03:10 AM 0
Share

but i know the length of the array and i debugged the number that comes out, and the current waypoint is picked up. I dont know what could possibly be the cause

avatar image Julien-Lynge · Mar 01, 2013 at 05:38 AM 0
Share

So show us your code with the debug statements in there, especially around line 100. Debug out the value of the if() conditional, the value of randWaypointTimed, the length of the array, and anything else that might be useful. Include the output from the console along with the error you're getting (the whole error, which includes a line number).

Hopefully with that $$anonymous$$imum effort, someone here will be able to help you. But please, don't just copy and paste your code and expect other people to make heads or tails of it.

Good luck!

avatar image LivingUniverse · Mar 01, 2013 at 05:46 AM 0
Share

but randwaypointtimed isnt being called, i have it commented out. As you can see it only runs when stopTimerRndSelect is false.

avatar image LivingUniverse · Mar 01, 2013 at 07:13 PM 0
Share

meh, i figured it out. The argument out of range was a lie. It was only argument out of range for the first second of starting the game, but after that there was no argument out of range. I fixed it. (i added a timer of 1.5 seconds before the first currentwaypoint is selected)

avatar image
2

Answer by The-IT664 · Mar 01, 2013 at 04:06 AM

Having a quick look through your code, my best guess would be that Random.Range(-1, 10) actually returns numbers in the range of -1 to 10.

From the documentation:

Returns a random float number between and min [inclusive] and max [inclusive] (Read Only).

http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html

Comment
Add comment · Show 2 · 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 LivingUniverse · Mar 01, 2013 at 04:18 AM 0
Share

but ive tried everything you can imagine in terms of numbers. Its not to do with that i dont think

avatar image sdgd · Mar 01, 2013 at 08:50 PM 0
Share

if you want numbers simple

 float x = 1.5;
 int y ;
 y = (int) x;
 

it'll return 1 iirc so giving +0.5 does the trick

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

12 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

Related Questions

A node in a childnode? 1 Answer

Is this an [optimal, adequate, ftarded] way to make a snake follow its head? 2 Answers

Waypoints and out of range problem.[Solved] 1 Answer

How to dynamically create buttons based on contents of a list using C#? 1 Answer

How do I get a list to randomly select.... 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