Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ZeroRadius · Nov 15, 2016 at 08:51 AM · c#out of range

Array index is out of range on simple C# script

So I have a stupidly simple script and I am getting an Idex out of range error on it and can't figure out what's wrong with it.

My code: using UnityEngine; using System.Collections;

 public class BoardMovement : MonoBehaviour {
 
     private int NextSpaceNum = 0;
     private Vector2 target;
     public Transform[] BoardSpots;
     public float MovementSpeed = 2f;
 
     void Start()
     {
         this.MovePiece(); //Testing MovePiece();
     }
 
     public void MovePiece()
     {
         this.target = this.BoardSpots[0].position;
         this.transform.position = Vector2.MoveTowards(this.transform.position, target, MovementSpeed * Time.deltaTime);
         
     }
 }


I hard coded the index in and I even tried changing "public Transform[] BoardSpots;" to "public Transform[] BoardSpots = new Transform[100];" but I still keep getting an out of range exception. I have dragged 3 Empty GameObject(Transform) 's into my array via the Inspector window. alt text

capture2.png (12.3 kB)
Comment
Add comment · Show 2
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 Desoxi · Nov 15, 2016 at 08:58 AM 1
Share

What is it logging when you use Debug.Log(BoardSpots.Length) ?

avatar image Desoxi Desoxi · Nov 15, 2016 at 08:58 AM 0
Share

Ah and try to get rid of those "this." prefixes. No need for them in your current code :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ZeroRadius · Nov 15, 2016 at 09:59 AM

oh wow now I feel stupid. I did the Debug.Log and found that it was logging twice so I went through all of my Game Objects and somehow the script got attached to another object that it should not have been attached too and of course sense I did not know it was there it was never initialized.

Also the "this" keyword is the proper way to refer to data that is a member of a class and will not hurt the code if it is there.

Thanks for the help ~Zero

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 Desoxi · Nov 15, 2016 at 10:26 AM 0
Share

Glad i could help. And yes the keyword wont hurt your code but is an unnecessary prefix which is worsening the readability of your code. Its just a well-meant advice, you dont have to follow it ;)

Here is a link to the C# docs which explains proper usage of the keyword: https://msdn.microsoft.com/en-us/library/dk1507sz.aspx

$$anonymous$$y general rule is to only use "this" when there is a variable in the local scope with the same name, else it is, as i said, just unnecessary.

Greetings Desoxi

avatar image ZeroRadius Desoxi · Nov 15, 2016 at 06:15 PM 0
Share

Actually using the "this" keyword makes your code more legible to people working on it in the future. Lets say Someone has a method that is 50 lines (a bit ridiculous and should probably be broken out into more methods but it happens). Lets say you are trying to troubleshoot the code and you find a variable called name and you need to know said variable scope. You could scroll through the method and look for it and then scroll to the top and look for it (or I guess ctrl + f but that also takes time) or You could look to see if it is using the "this" keyword, if it is using the "this" keyword you know that it is a instance variable rather than a local variable. You would use "this" as well to prefix a call to a method inside of the class so that A) you know they are not using a method from another class that was instantiated somewhere in this mess of code and B) If it is an inherited class the method might not even be shown in the code because it did not need to be overridden. The "this" keyword is used strictly to make your code more readable and should be used just as liberally as comments (my code did not have comments because I slapped it together quickly and wanted to get it working before I commented it, now that it works almost every line will be commented so in six months when I come back I can easily tell what's going on)

avatar image Desoxi ZeroRadius · Nov 15, 2016 at 06:31 PM 0
Share

I see, you know what you are doin. I think its just personal flavor then. Btw, if you need to scroll or use ctrl+f to find a method/variable in any code, you should search for another IDE or when you are using visual studio, make use of the F12 key, which is the shortcut for "go to definition". Definitely saves some time.

And this was not meant as a fight btw :) If you are coding how you do, just keep going. Was not trying to offend your style in any way ;)

Cheers!

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Editor Script, Index Out of Range Exception after Play 1 Answer

Renderer on object disabled after level reload 1 Answer

generic list, swapping equipment errors but not always? 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