- Home /
For statement errors?
So, I am working on a parenting script that will make an array of objects parent to another object. Before I go any further, here is my script-
var allforces : GameObject [];
var editmode = true;
function Update () {
allforces = GameObject.FindGameObjectsWithTag("force");
if(Input.GetKeyDown("space")){
editmode = !editmode;
if(editmode == true) { //what happens if the player switches from edit mode to play mode
Gotoedit ();
}
}
}
function Gotoedit () {
for(var i : int = 0; !i = allforces.length; i++) {
allforces[i].transform.parent = GameObject.FindGameObjectWithTag("fuse");
}
}
The first part of the script works perfectly (function Update), but the (function Gotoedit) does not work. I keep getting the errors
Assets/Standard Assets/Scripts/modemanager.js(21,56): BCE0043: Unexpected token: ).
Assets/Standard Assets/Scripts/modemanager.js(21,33): BCE0044: expecting ;, found '='.
This is very annoying, as I cannot work on the script (or the game, for that matter) until I have fixed the errors. This is my first time messing around with FOR statements, so pardon me if this is an noob mistake. I got all my references from the following links-
http://unity3d.com/learn/tutorials/modules/beginner/scripting/loops
http://answers.unity3d.com/questions/11547/how-do-i-use-for-to-create-loops-in-my-script-and.html
I seem to have gotten the form of the FOR statement right, but I remain getting the same result. Could someone PLEASE figure out what I'm doing wrong?
*Javascript only, please
*Thanks
Answer by ffxz7ff · Jan 31, 2014 at 02:27 AM
You probably want
!i == allforces.length
instead of
!i = allforces.length
== is a comparison, = is an assignment.
He probably want to use
i < allforces.length
ins$$anonymous$$d since it's shorter, the most common for statement condition and more safe than checking for inequality.
btw inequality has it's own operator:
i != allforces.length
ffxz thanks a lot! I actually knew that, I must have missed it. Thanks!
I changed it to
function Gotoedit () {
for(var i : int = 0; !i < allforces.length; i++) {
allforces[i].transform.parent = GameObject.FindGameObjectWithTag("fuse");
}
}
as that seems to suit my purpose better. Thanks for the speedy reply, I needed that tonight!
Your answer
Follow this Question
Related Questions
For loop not working? 1 Answer
Unity says that my script isn't doing anything? 1 Answer
For statement stops function? 1 Answer
Collision of Character collider not working? 1 Answer
Help solving for loop error 1 Answer