Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

these codes are hanging unity.

Discussion in 'Scripting' started by sarthakz9, Jun 17, 2015.

Thread Status:
Not open for further replies.
  1. sarthakz9

    sarthakz9

    Joined:
    Jun 3, 2015
    Posts:
    19
    When i'm using these codes,

    void OnGUI()
    {
    While(input.key("p"))
    {
    Shoot =1;
    Power+=5;
    If(power>40)
    {Power=40;
    Break;}
    }
    }

    These codes are giving no errors. But if i press p during run time. Unity hangs and i have to force close it..

    So what should i do ?
     
    Last edited: Jun 17, 2015
  2. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,055
    Dont use a while loop, use an 'if' statement.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. Haseeb_BSAA

    Haseeb_BSAA

    Joined:
    Aug 20, 2014
    Posts:
    316
    Why are you using While Loops? I think this can easily be done by using an IF statement. But while loop shouldn't be a problem anyways. Try to put this script on another object and see if it's working fine or not. Maybe another project.
    Also use the code tags from next time. Makes code easier to read. In text editor , INSERT > CODE.
     
  5. sarthakz9

    sarthakz9

    Joined:
    Jun 3, 2015
    Posts:
    19


    Well,I want to know ,what is wrong with this code .
     
  6. sarthakz9

    sarthakz9

    Joined:
    Jun 3, 2015
    Posts:
    19
    If statement is working, but not the way i want. I can't use the "break" part in if statement.
    I want ,when user press p, the power increase and when he release the key the loop ends (simple)...
    The question is , is it turning into an infinite loop ,or something like that ?
     
    Haseeb_BSAA likes this.
  7. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    I'm surprised this code compiles at all.
     
  8. sarthakz9

    sarthakz9

    Joined:
    Jun 3, 2015
    Posts:
    19
    You can try it.:cool::D
     
  9. Haseeb_BSAA

    Haseeb_BSAA

    Joined:
    Aug 20, 2014
    Posts:
    316
    A SWITCH statement can do exactly the same thing. It has a break function.
    But trust me you can do that with an IF statement too , like that :
    Code (CSharp):
    1.  
    2. public bool loopEnd;
    3.  
    4. public int p;
    5.  
    6. void Update()
    7. {
    8.   if(Input.GetKey(Keycode.Space)
    9.    {
    10.       loopEnd = false;
    11.        p++;
    12.     }
    13.    else
    14.      {
    15.        loopEnd = true;
    16.      
    17.      
    18.       }
    19.  
    20.      If(loopEnd == true)
    21.         {
    22.            print(p);
    23.          }
    24. }
     
    sarthakz9 likes this.
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
    Input.GetKey returns true on every frame that key is down.

    One frame is the time it takes for all of the Update functions to run.

    Your while-loop will keep running until the key isn't registered as down anymore, and prevent the Update from finishing. Since your Update needs to finish before the key is registered as down, the while-loop will never exit.
     
  11. Haseeb_BSAA

    Haseeb_BSAA

    Joined:
    Aug 20, 2014
    Posts:
    316
    True that!
     
  12. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    792
    also impressed it does anything - you're using capital V on void to start your function there.. keep an eye out for that.
     
  13. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
    "void start" is a lot worse, as it compiles and does nothing. I'm pretty sure OP's code isn't copy-pasted, though.

    hey, @willgoldstone , maybe a console warning about Unity-Methods written with lowercase letters could be implemented? I've probably answered half a million questions on UA and here where the solution was "put a big U on Update".
     
    Haseeb_BSAA and Dantus like this.
  14. sarthakz9

    sarthakz9

    Joined:
    Jun 3, 2015
    Posts:
    19
    I didn't copy paste it.:confused::mad:
    I wrote this code on my phone.
     
    Haseeb_BSAA likes this.
  15. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I think I'll just lock this thread. Come back when serious :)
     
    blizzy and VoidChimera like this.
Thread Status:
Not open for further replies.