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 Therian13 · May 24, 2015 at 02:48 AM · arrayloopwhilemodulespercentage

while loop & array- turn on something via percentage

Hello Everyone,

I am currently creating a script that makes rocks fall one at a time by the amount of percentage is complete.

The problem is, it can only be done on very specific numbers. (currently = modulus has to equal zero to turn on falling rock). while the script is able to handle any number of rocks, if I change the speed of how fast the percentage goes up, it can skip rocks, making them fall out of order, or not at all.

Is there a way I can re-write the while loop to have it drop each rock based off of percentage and the number of rocks I have?

The while loop is under the refillRate() method. Here is my script:

 #pragma strict
 var rate : float;
 var curProgress : float = 0;
 private var maxProgress : float = 100;
 private var drawGUI = false;
 private var percentOfProgress : float;
 var progressBarLength : float;
 private var maxProgressBarLength : float = 100;
 
 var progressTexture : Texture2D;
 var progressTextureMain : Texture2D;
 var canDecrease : boolean = false;
 //var screenup : float;
 //var screenside : float;
 var excavateStyle : GUIStyle;
 var rocks : Rigidbody[];
 private var check : boolean = true;
 private var i : int = 0;
 private var percentage : float;
 
 function Start () 
 {
 percentage = (100/rocks.length);
 }
 
 function Update () 
 {
 if (drawGUI == true && Input.GetKey(KeyCode.E))
         {
             refillRate();
         }
 else if (canDecrease == true)
         {
         defillRate();
         }
 
          percentOfProgress = curProgress/maxProgress; //current % in decimal
         progressBarLength = percentOfProgress*100; // current %
     if (curProgress > maxProgress)
         {
         curProgress = maxProgress;
         }
     if (curProgress < 0)
         {
         curProgress = 0;
         }
  
  
 }
 
 function refillRate()
 {
         curProgress+= rate/100;         rocks[0].isKinematic = false;
         
         while(progressBarLength % percentage == 0)
          {
          //rocks[i] = GetComponent.<Rigidbody>().isKinematic = true;
          
              if (check == true)
              {
              check = false;
              i++;
              rocks[i].isKinematic = false;
              yield WaitForSeconds (0.1);
              check = true;
              }
              else
              {
              Debug.Log("out of range/time");
              }
          
              if ( i >= rocks.length-1)
                  {
                      i = 0;
                  } 
                  
  }
 
 }
 function defillRate()
 {
         curProgress-= (rate/100)/2; //fills up the progress meter.
 
 }
 function OnTriggerEnter (theCollider :Collider) 
 {
     if (theCollider.tag == "Player")
     {
         drawGUI = true;
     }
 }
 function OnTriggerExit (theCollider :Collider) 
 {
     if (theCollider.tag == "Player")
     {
         drawGUI = false;
     }
 }
 
 function OnGUI()
 {
 
     if (drawGUI == true)
         {
         GUI.Box (Rect (Screen.width*0.5, Screen.height*0.3, 300, 45), "Hold E to Excavate",excavateStyle);
         GUI.DrawTexture(Rect((Screen.width/2.5), Screen.height/10, maxProgressBarLength *Screen.width/650, 50), progressTextureMain);
         GUI.DrawTexture(Rect((Screen.width/2.5), Screen.height/10, progressBarLength *Screen.width/650, 50), progressTexture);
         }
         
     //calculate bar length, based off of how much is completed.
 
 }
Comment
Add comment
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

1 Reply

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

Answer by JoeStrout · May 24, 2015 at 02:53 AM

Yes -- your code here is working much too hard. Don't use coroutines; they'll just confuse you. Simple keep track of how much "percentage" (work of some sort?) still has to be done before you spawn the next rock. Update this in your Update() method, and when this becomes

No need for while/yield, and no possibility of skipping a rock or spawning them out of order.

Comment
Add comment · Show 3 · 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 Therian13 · May 24, 2015 at 03:19 AM 0
Share

the problem is that each scene has a different number of rocks in them, some have 6 while some can have 20.

by using the while loop/array, I can have it check each one individually, ins$$anonymous$$d of me having to make a new script for scene telling it to turn off each one at a set percentage.

it's not spawning rocks either, it just turns off is$$anonymous$$inematic to make them fall again on objects already in the scene.

But as you stated "Simple keep track of how much "percentage" (work of some sort?) still has to be done before you spawn the next rock" this is what I'm trying to accomplish with the script. I'm trying to find a way for the script to calculate how much percentage in increments would be needed before the next rock should fall, while taking into account the array.length.

avatar image Therian13 · May 24, 2015 at 04:16 AM 0
Share

@JoeStrout After I took a break and re-read what you said, it finally clicked. Think I've been staring at the computer too long for it to process. But I took your advice and changed out the while loop with an if statement. Works like a charm now! Here is what I did to fix it.

I changed the refillRate() method.

it is now:

 function refillRate()
 {
         curProgress+= rate/100; //fills up the progress meter.
         
         if ( progressBarLength >= percentage*(i+1) ) //checks how many objects we have, and then increases percentage needed to drop next object.
          {
                       if (check == true)
              {
              check = false;
              
              rocks[i].is$$anonymous$$inematic = false;
              i++;
              yield WaitForSeconds (0.1);
              check = true;
              }
              else
              {
              Debug.Log("out of range/time");
              }
          
              if ( i > rocks.length-1)
                  {
                      i = 0;
                  } 
                  
  }
 
 }

avatar image JoeStrout · May 24, 2015 at 01:40 PM 0
Share

Looks good! But you should no longer need the yield WaitForSeconds bit.... nor all that stuff about "check"... there's more cruft here you can cut away, but you've basically got it!

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

20 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 avatar image avatar image avatar image avatar image

Related Questions

Trouble checking against array objects 0 Answers

Array loop first element goes last? 1 Answer

Trouble with for loop out of range. Simple? Maybe. 1 Answer

How to compare and detect overlapping gameObject Prefabs using maths 0 Answers

While loop crashing 2 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