- Home /
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.
What is it logging when you use Debug.Log(BoardSpots.Length)
?
Ah and try to get rid of those "this." prefixes. No need for them in your current code :)
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
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
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)
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!