- Home /
For loop is being totally ignored
I've posted a script I'm using in a game, the script essentially draws a laser beam from a laser gun to a target using a line renderer and then instantiates some point lights along the beam's path to give it a glow effect on nearby objects. It's a little lengthy and I'm only needing help on a small part of it, but to avoid ambiguity I've just included the whole thing. At line 50 I have a for loop that is supposed to be in effect if the variable "firing" is true, which I currently just turn on and off manually in the editor. Everything in the script works fine, but for some reason the game totally ignores the for loop at line 50. I don't get any error messages, and I've even tried commenting out the rest of the loop except for the print line for debugging purposes, and it never prints that line, no matter what.
I'm sure it's not a matter of the "noOfLightsNeeded" variable being zero or anything like that, I've already made sure it reaches a positive integer greater than 1. Any ideas?
var lineRenderer : LineRenderer;
var laser : GameObject;
var target : GameObject;
var laserMaterial : Material;
var firing : boolean = false; // Is laser firing?
var laserGlowLight : Light; // Light prefab for glow effect
var distToTarget : float;
var noOfLightsNeeded : int;
var currentNoOfLights : int;
private var i : int = 0;
function Start () {
}
function Update () {
if (i == 0) // Initial instance of line renderer at game start. For some reason, function Start () wasn't working.
{
lineRenderer = gameObject.AddComponent(LineRenderer);
lineRenderer.material = laserMaterial;
lineRenderer.castShadows = false;
lineRenderer.receiveShadows = false;
lineRenderer.SetWidth(0.2,0.2);
lineRenderer.SetVertexCount(2);
i = 1;
}
if (firing) // Enables laser if firing.
{
lineRenderer.enabled = true;
lineRenderer.SetPosition(0,laser.transform.position);
lineRenderer.SetPosition(1,target.transform.position);
distToTarget = Vector3.Distance(laser.transform.position, target.transform.position);
noOfLightsNeeded = Mathf.Round(distToTarget);
if (noOfLightsNeeded > currentNoOfLights) // Adds lights along the beam as necessary
{
Instantiate (laserGlowLight, transform.position, transform.rotation);
currentNoOfLights++;
}
if (noOfLightsNeeded < currentNoOfLights) // Removes lights along the beam as necessary
{
GameObject.Find("Laser Fire Glow(Clone)").GetComponent(Laser_Glow_Destroy).exists = false;
currentNoOfLights--;
}
for (var q = 1; q == noOfLightsNeeded; q++)
{
print ("For loop works.");
var pointAlongRay : Vector3;
var ray : Ray;
var hit : RaycastHit;
var direction : Vector3 = laser.transform.position - target.transform.position;
if (Physics.Raycast (laser.transform.position, direction, hit))
{
print ("Working on Point "+q+".");
Debug.DrawLine (laser.transform.position, hit.point);
pointAlongRay = ray.GetPoint(q);
GameObject.Find("Laser Fire Glow(Clone)").transform.position = pointAlongRay;
}
}
}
if (firing == false) // Disables line renderer.
{
lineRenderer.enabled = false;
noOfLightsNeeded = 0;
if (currentNoOfLights > noOfLightsNeeded)
{
GameObject.Find("Laser Fire Glow(Clone)").GetComponent(Laser_Glow_Destroy).exists = false;
currentNoOfLights--;
}
}
}
That's impossible, I'm sure numberOfLightsNeeded does get up over 1, but what is it actually right before the for loop gets called? Has to be 1. I don't know where you printed out its value but print it on the line immediately before the for loop and you will probably see that it is 1
Nope, I tested it from 30m away and noOfLightsNeeded gets up to 30, even right before the loop. See line 35, it sets that value equal to the rounded integer value of the distToTarget, well before the loop starts.
Answer by robertbu · Aug 11, 2013 at 06:24 PM
So the issue is this line:
for (var q = 1; q == noOfLightsNeeded; q++)
If you have exactly one light, this like will execute once. Anything else, and it won't execute at all. You probably want:
for (var q = 1; q <= noOfLightsNeeded; q++)
Depending on geometry, this may not get you what you want. That is, it will calculate points along the ray one unit apart. This seems like a long distance for many geometry setups.
You're a genius, sir. Very much obliged. Also, yeah, the script certainly needs some fine tuning to make things work efficiently while still working effectively. I just needed it to run in the first place, and that was absolutely where the problem was. Thanks again!
Your answer

Follow this Question
Related Questions
Android multitouch problem 1 Answer
For loop not working 1 Answer
Trouble with for loop out of range. Simple? Maybe. 1 Answer
crazy loop !!! HELP ME 1 Answer
x=x Assignment made to same variable 3 Answers