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 srafciger · Nov 02, 2011 at 01:33 AM ·

Power charger script problem

I have a problem with my script, so I would appreciate if someone could help me.

It is a power charger. It should go automatic from 1 to 200, and thend go back from 200 to 1. Here's the code:

 var att1 : int = 10;
 
 function Update() {
 
 do  {att1+=Time.time;}
 while(att1<=200);
 
 if(att1==200){
 do {att1-=Time.time;}
 while(att1>=0);}
 
 }
 
 function OnGUI () {
 GUI.Box(Rect(155,202.8,att1*2,15),"");
 }

Every time I run it, it goes from 1 do 200 and then whole Unity freezes

Comment
Add comment · Show 1
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 syclamoth · Nov 02, 2011 at 12:15 PM 0
Share

This happens because the Update runs once per frame- it is already managing the looping behaviour for you! If you have a while loop using something which doesn't change until the end of the frame (for example, Time.time, or Input.GetButton("Whatever")), then it will always freeze up.

2 Replies

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

Answer by BerggreenDK · Nov 02, 2011 at 12:19 PM

first declare your custom state for this pattern/statemachine

 public enum PowerState
 {
    PowerUp,
    PowerDown
    // easily add more states if you need them here
 }
 
 PowerState chargerState = PowerState.PowerUp;
 
 

in your update or where you need to check it, use a SWITCH instead of IF. Because its easier to maintain and build out with multiple states

 switch (chargerState)
 {
 case PowerState.PowerUp:
    att1 += Time.deltaTime;
    if (att1 >=200) chargerState = PowerState.PowerDown;
 break;
 
 case PowerState.PowerDown:
    att1 -= Time.deltaTime;
    if (att1 <0) chargerState = PowerState.PowerUp;
 break;
 
 //  add more states if you want more interesting patterns
 
 default: // if no other conditions are true
     Debug.Log("unknown state: " + chargerState);
 break; // always remember to end with a break pr. case, also default case
 
 }
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 srafciger · Nov 02, 2011 at 01:21 PM 0
Share

useful too !!!!!

avatar image srafciger · Nov 03, 2011 at 09:56 PM 0
Share

I decided to mark your question as correct answer ,because I decided to put up one more state, when you press certain key that charger stops and saves current value.

avatar image BerggreenDK · Nov 06, 2011 at 08:48 PM 0
Share

cool! thanks. I just wanted to show you another approach than using IF...THEN, because state-machines are so much more fun to scale up.

avatar image
0

Answer by Anxo · Nov 02, 2011 at 05:21 AM

That is because you are getting stuck in a while loop. You dont need "while" in this case.

 Poweringup : boolean = true;
 
 if(att1 <=0)
 {
 Poweringup = true;
 }
 
 if(Poweringup)
 {
 att1+=1*Time.deltaTime;
 }
 if (att1 >=200)
 {
 Poweringup = false;
 }
 if(!Poweringup)
 {
 att1-=1*Time.deltaTime;
 }
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 srafciger · Nov 02, 2011 at 11:15 AM 0
Share

Thanks...Is there any way to do this without boolean.Just curious

avatar image syclamoth · Nov 02, 2011 at 11:52 AM 0
Share

... why? What have you got against booleans?

avatar image BerggreenDK · Nov 02, 2011 at 12:12 PM 0
Share

yes there is. check my answer, will write it soon.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Sound script problem. 0 Answers

problem with my simple script please help. 2 Answers

Car wheel problems 1 Answer

problem with raycasts and tags 1 Answer

Accessing variable from another script 1 Answer


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