- Home /
The question is answered, right answer was accepted
Trouble with for loop out of range. Simple? Maybe.
So I have been staring at this code trying to figure out why it's out of range.
I have 4 empty game objects as children in another empty object. Those 4 are tagged as "waypoint". The parent has no tag. I am trying to store these 4 points in an array, then fill another array with their Vector3 coords to use as a waypoint system. Please help? Probably an easy solution right in front of my face but I can't see it.
function Start(){
amountToMove = amountToMove * Time.deltaTime;
waypoints = GameObject.FindGameObjectsWithTag("waypoint");
for(var i : int = 0; i <= waypoints.length; i++){
waypointVectors[i] = waypoints[i].transform.postion;
}
}
For some reason the forums is not letting me list the things I have tried..it is cutting my post in half. Lets try again.
I have tried... i
Have you tried debugging to see which list the index is out of range in?
I have but have been unsuccessful in getting an actual print on the console.
function Start(){
amountTo$$anonymous$$ove = amountTo$$anonymous$$ove * Time.deltaTime;
waypoints = GameObject.FindGameObjectsWithTag("waypoint");
Debug.Log(waypoints.length);
//for(var i : int = 0; i <= waypoints.length; i++){
// waypointVectors[i] = waypoints[i].transform.postion;
//}
}
No printouts. I feel silly.
what type is waypoints? Are you getting any errors with the debug?
Well, your error is obvious: your trying to set a Vector3
, that doesn't exist, in the empty list waypointVectors
to something. Since you can't change the size of Built in Lists, I suggest you use another type for your purpose. Google is your friend when it comes to this
The reason for the absent logs.... ID$$anonymous$$ Try just making an empty file that just logs stuff... BTW you do know that the logs show up in the error console, right?
Answer by EliteMossy · Apr 08, 2013 at 08:29 AM
This should hopefully fix it
function Start(){
amountToMove = amountToMove * Time.deltaTime;
waypoints = GameObject.FindGameObjectsWithTag("waypoint");
//Always! Always! Instaniate an array.
waypointVectors = new Vector3[waypoints.length];
//You do not want to use <= as this will cause an out of range error.
//i explained it in the bottom of my answe
//for(var i : int = 0; i <= waypoints.length; i++){
for(var i : int = 0; i < waypoints.length; i++){
waypointVectors[i] = waypoints[i].transform.position;
}
}
Also are you sure its attached to an Active GameObject in the scene?
Regarding this line: for(var i : int = 0; i
What is happening is, that the waypoints.length will tell you the current amount of elements, so for this example say 10. Now you may think, 1 2 3 4 5 6 7 8 9 10, perfect! But you are wrong, it starts at 0!. So 0 1 2 3 4 5 6 7 8 9, see we have no 10! Now the check you had "" is testing for less than or equal to waypoints.length, the problem is, waypoints.length is showing 10, but we know the max is 9! Hence the reason we should only check for if it is less "<" than 10, hence 9.
Thank you, this eli$$anonymous$$ated my out of range error but now I get a null error on the line waypointVectors[i] = waypoints[i].transform.postion;
NullReferenceException: Object reference not set to an instance of an object.
Hmm unless you have a ton of waypoints or something I'm not sure why you are trying to use such a complex system for a simple problem.
Why don't you just have one int which refers to the 4 waypoint options:
int waypointInt = 1; // first waypoint
int waypointInt = 2; // second waypoint etc...
Then set up a vector position for each waypoint like:
Vector3 vectorPositionOne = new Vector 3 (50, 2, 50); // etc
And then you could just do a series of if statements to deter$$anonymous$$e the correct waypoint:
if (waypointInt !=0)
{
if (waypointInt == 1)
{
transform.position = vectorPositionOne;
return;
}
else if (waypointInt == 2)
{
transform.position = vectorPositionTwo;
return;
}
//etc..
}
I am new to code so I tend to avoid overly complex solutions. There could be benefits to your intended method but unless your game is extremely heavy on performance I doubt anyone would notice.
@RyanZimmerman87 Problem with this approach is expand-ability. This looks really ugly and could be come a complete mess in the future.
Well Elite, I get unknown identifier "string" errors on your debug entries. Changed them to "String" and still get the same null error with no other info.. I also removed the random c at the end! The more I work at this the more I want to find an alternative solution.
This is a 2d game, I am trying to setup a waypoint system to move my boss around the screen.
No idea why string.Format does now, unless you don't have import System; or something. I use C# not unityscript(js) so i am not sure on its limitations.
Follow this Question
Related Questions
Method to loop through an unknown number of variable combinations in C# 1 Answer
Loop through array until certain value is found. 2 Answers
Using a for loop to generate and populate an array 1 Answer
I dont know why im getting a null reference exception 1 Answer
How can I move back to Element 0 in an array to reset a loop? 1 Answer