- Home /
For statement stops function?
I am working on a physics building game, and I need to switch to play mode from edit mode. When I switch, I need to disable the sphere colliders on all gameobjects with the tag "probe", and connect all the parts to the fuselage (tagged "fuse"). Here is the script-
var forces : GameObject[];
var probes : GameObject[];
function Update () {
forces = GameObject.FindGameObjectsWithTag("force");
probes = GameObject.FindGameObjectsWithTag("probe");
if(Input.GetKeyDown("l")) {
Disablescols ();
Enablephysics ();
GameObject.FindGameObjectWithTag("fuse").AddComponent(Rigidbody);
}
}
function Enablephysics () {
for(var i = 0; i <= forces.length; i++){
forces[i].AddComponent(Rigidbody);
forces[i].AddComponent(HingeJoint);
forces[i].GetComponent(HingeJoint).connectedBody = GameObject.FindGameObjectWithTag("fuse").rigidbody;
forces[i].GetComponent(HingeJoint).spring.damper = Mathf.Infinity;
}
}
function Disablescols () {
for(var k = 0; k <= probes.length; k++){
probes[k].GetComponent(SphereCollider).enabled = false;
}
}
The script is executing everything well, except it does not add the rigidbodies and does not add the joints. In short, it is not executing Enablephysics ();. Is Unity stopping the script after the FOR statement in Disablescols () met? Can someone help me?
*Javascript only, duh
*thanks
EDIT
I have fixed the problem, with the help of the people that answered. The Debug.Log was not going through, as it was turned off in the console (I don't know why???). Also, the order in which the functions were being called was wrong. Instead of going
Disablescols
Enablephysics
GameObject.FindGameObjectWithTag("fuse")
It had to go
GameObject.FindGameObjectWithTag("fuse")
Disablescols
Enablephysics
as the enablephysics () function requires a rigidbody to work.
Thanks guys!!
yes, there are no errors, and i have tested quite a bit with Debug.Log
The only way Enablephysics
does not get called, is when Disablescols
throws an error (Yes, it's the ONLY way, other than the entirety of Unity crashing)
I think you turned off the Error printing in the console by toggling that Error key.
Answer by adnirjhar · Feb 01, 2014 at 09:32 PM
I think the code is breaking here ->
for(var k = 0; k <= probes.length; k++)
Because you are checking upto equal. Once the code breaks, it wont continue executing with the right behaviour.
Not sure though. :P
I mean you are supposed to check upto probes.length-1 as you are starting from 0. Shouldn't it be something like ?-
for(var k = 0; k <= probes.length-1; k++)
@adnirjhar which simplifies to just:
for (var k = 0; k < probes.length; k++)
Your answer
Follow this Question
Related Questions
For loop not working? 1 Answer
For statement errors? 1 Answer
Collision of Character collider not working? 1 Answer
Why is rigidbody.AddRelativeTorque not working? 1 Answer
How do I reset a for loop variable??? 3 Answers