- Home /
Simple array usage
Can someone please tell me why this code doesn't work and show me how to fix it?
var TileNumberArray; TileNumberArray = new int(); TileNumberArray.Length = 80;
static var ResetButtonSetting = 1;
function Update () { for(TileArrayChoice=1;TileArrayChoice < TileNumberArray.Length; TileArrayChoice++); { TileNumberArray[TileArrayChoice] = ResetButtonSetting; }
}
I'm a noob at programming but this really should work as far as I can tell. The error code I get is as follows;
resetDialScript.js(12,42): BCE0044: expecting :, found '='.
Any help would be greatly appreciated.
"It's enough to make you chew your own foot off" - John Cleese
Answer by Bampf · Aug 16, 2010 at 01:24 PM
Remove the extra semicolon on the line where the loop is declared:
var TileNumberArray; TileNumberArray = new int(); TileNumberArray.Length = 80;
static var ResetButtonSetting = 1;
function Update () { for(TileArrayChoice=1;TileArrayChoice < TileNumberArray.Length; TileArrayChoice++) // ; <-- removed { TileNumberArray[TileArrayChoice] = ResetButtonSetting; }
}
== [Addendum] Now that the script actually compiles you will be able to run it. This will give you some new errors which indicate that early responses you got were also on the right track: the length parameter should not be capitalized, and the array declaration should be
TileNumberArray = new Array();
For reference, see the documentation page on Arrays.
Cheers for trying but still no luck, same error message.
resetDialScript.js(12,42): BCE0044: expecting :, found '='
Which refers to this line
TileNumberArray[TileArrayChoice] = ResetButtonSetting;
Cheers, took unity a while to realise the script had changed, lol. Now I have something else to think on. Again thanks to all the people who have posted such quick replies, I am in your debt :-)
No problem. Don't forget to vote up any replies that you find helpful, and (when possible) mark the checkbox on the one that best solved your problem.
Answer by 3dDude · Aug 16, 2010 at 01:01 PM
ok well there are lots of problems in our code...
1# array.length is not capitalized.. and it is read only so you cant mod it in scripts;
2# your var should look like this:
var TileNumberArray : int[];
i'm not sure if these problems are the real problems because i'm a noob to ;).
Answer by vyruz · Aug 16, 2010 at 01:05 PM
hey there :)
i think array length is readonly!
and you cant say array=single_value
use array[index]=value instead
for (int x=0;x< TileNumberArray.Length;x++)
i would suggest learning programming concepts VERY WELL b4 diving into the deep sea of unity :) good luck!
Would it be possible for you to post a fixed example of the script? Cheers :-)
Answer by IBDelta · Aug 16, 2010 at 01:10 PM
Thanks for trying but there is no problem with the "Length" parameter, only the first letter needs to be capitalised, several variations show this is the only spelling that unity recognises as an internal command-thingy.
Removing the "new" before "int[]" has done nowt.
The problem points to this line;
TileNumberArray[TileArrayChoice] = ResetButtonSetting;
Again, your help was appreciated, ty
FYI, you should delete this answer and post it again as a comment on the original answer. Thanks
Answer by IBDelta · Aug 16, 2010 at 01:55 PM
Again, huge thanks to all those who have helped. For the sake of completion here is the finished code that provides no errors during runtime or otherwise;
var TileNumberArray; TileNumberArray = new Array();
var TileArrayChoice; static var ResetButtonSetting = 1;
function Update () { for(TileArrayChoice=0;TileArrayChoice<81; TileArrayChoice++) { TileNumberArray[TileArrayChoice] = ResetButtonSetting; }
}
whether it actually does what I expect it to do remains to be seen :-)
Again, a huge thanks to all
"I can see clearly now the rain has gone, I can see all obstacles in my way" - Johnny Nash
Ins$$anonymous$$d of looping to 81, you should loop until TileArrayChoice >= TileArrayChoice.Length. This lets you change the size of the array (either in code or the inspector) without having to change any code.
Your answer
Follow this Question
Related Questions
Script error. Please Help! 4 Answers
'If' Statement in javascript giving me problems 3 Answers
BCE0044: expecting ':' found ';' 1 Answer
Basic on collision play animation code not working 1 Answer
BCE0044: expecting :, found '=' 3 Answers