- Home /
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();
}
}
Answer by Julien-Lynge · Mar 01, 2013 at 02:08 AM
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.
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
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!
but randwaypointtimed isnt being called, i have it commented out. As you can see it only runs when stopTimerRndSelect is false.
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)
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
but ive tried everything you can imagine in terms of numbers. Its not to do with that i dont think
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