Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by growling_egg · Aug 11, 2013 at 06:13 PM · loopfor loopfor

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--;
         }
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Slobdell · Aug 11, 2013 at 06:31 PM 0
Share

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

avatar image growling_egg · Aug 11, 2013 at 06:37 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image growling_egg · Aug 11, 2013 at 06:27 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges