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 /
This question was closed Apr 08, 2013 at 08:34 AM by Fattie for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by BDP1 · Apr 08, 2013 at 07:43 AM · arrayloopfor loopout of range

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;
     }
 
 }
 
Comment
Add comment · Show 13
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 BDP1 · Apr 08, 2013 at 07:38 AM 0
Share

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

avatar image Benproductions1 · Apr 08, 2013 at 07:47 AM 0
Share

Have you tried debugging to see which list the index is out of range in?

avatar image BDP1 · Apr 08, 2013 at 07:52 AM 0
Share

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.

avatar image Benproductions1 · Apr 08, 2013 at 07:55 AM 0
Share

what type is waypoints? Are you getting any errors with the debug?

avatar image Benproductions1 · Apr 08, 2013 at 08:17 AM 1
Share

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?

Show more comments

1 Reply

  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 10 · 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 BDP1 · Apr 08, 2013 at 08:37 AM 0
Share

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.

avatar image RyanZimmerman87 · Apr 08, 2013 at 08:45 AM 0
Share

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.

avatar image EliteMossy · Apr 08, 2013 at 08:47 AM 0
Share

@RyanZimmerman87 Problem with this approach is expand-ability. This looks really ugly and could be come a complete mess in the future.

avatar image BDP1 · Apr 08, 2013 at 08:54 AM 0
Share

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.

avatar image EliteMossy · Apr 08, 2013 at 08:57 AM 0
Share

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.

Show more comments

Follow this Question

Answers Answers and Comments

14 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

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


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