Search Unity

Question Problem With Code

Discussion in 'Scripting' started by lockesky000, Dec 6, 2022.

  1. lockesky000

    lockesky000

    Joined:
    Dec 6, 2022
    Posts:
    5
    I am taking coding as a class and I am having troubles with a small piece of code that keeps freezing up, the idea of the code is to create a while loop and an imbedded while loop in order to count to one hundred, but the game keeps freezing every time I run it. I think that for some reason the loop is not ending and that is causing the problem but I rewrote the code several times and can't fix the problem at all. Where am I going wrong?
    int a = 0;
    int b = 0;
    int c = (b * 10);
    int d = (c + a);
    int e = 10;
    while (d < 100)
    {
    a++;
    if (a >= e)
    {
    b++;
    a -= e;
    }
    print (d);
    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Is this Unity and C# ?
     
  3. lockesky000

    lockesky000

    Joined:
    Dec 6, 2022
    Posts:
    5
    Yes it is
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    You're creating an infinite loop. Can you see that "d" is always less than 100 so it'll loop forever?

    BTW: Here's how to post code on the forums using code-tags.
     
  5. lockesky000

    lockesky000

    Joined:
    Dec 6, 2022
    Posts:
    5
    The code inside the while loop should be adding to the values of A and B and since A and B are a part of C (and then a part of D) eventually the value of D should go up to 100. At least that's what I planned.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    "d" won't change unless you change it explicitly. "d" is not a function. It won't always be "int d = (c + a);" as "c" and "a" change.

    In your above code befoer the loop begins:
    a is 0
    b is 0
    c is 0 * 10;
    d is 0 + 0;
    e is 10
     
  7. lockesky000

    lockesky000

    Joined:
    Dec 6, 2022
    Posts:
    5
    So would writing a line of code that updates the value of 'd' inside of the while loop work?
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    You have to. D only updates when you update it.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    This! ^ ^ ^

    Unity will lock up 100% of the time EVERY millisecond your scripting code is running.

    Nothing will render, no input will be processed, no Debug.Log() will come out, no GameObjects or transforms will appear to update.

    Absolutely NOTHING will happen... until your code either:

    - returns from whatever function it is running

    - yields from whatever coroutine it is running

    As long as your code is looping, Unity isn't going to do even a single frame of change. Nothing.

    No exceptions.

    "Yield early, yield often, yield like your game depends on it... it does!" - Kurt Dekker
     
  10. lockesky000

    lockesky000

    Joined:
    Dec 6, 2022
    Posts:
    5
    Got it to run without crashing, thank you!
     
    Nad_B likes this.
  11. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    You need to learn the different C# data types.
    Int
    (and all other primitives like
    float
    ,
    bool
    ...etc) are called
    Value Types
    .
     
  12. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    Also worth noting if you want
    d
    to always equal
    c + a
    , then you could make it into a property:

    Code (CSharp):
    1. public int D { get { return c + a; } }
     
    Bunny83 and Nad_B like this.
  13. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Note that a property should live at a class level, as you can't declare one inside a method.
     
    Bunny83 likes this.
  14. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    Yeah I realise now that all that unformatted code is likely scoped to a method.
     
    Bunny83 and Nad_B like this.
  15. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,000
    What kind of coding class? Your code looks like you come from a functional programming language like Haskell where you do not define a sequence of operations but you just define relationships. This is not the case for most imperative programming languages. Functional programming languages are way more abstract and detached from how the hardware actually works. Most imperative languages could almost be seen as a machine language on steroids covered in tons of sugar :)

    Just to provide a working example for future readers
    Code (CSharp):
    1. int a = 0;
    2. int b = 0;
    3. int d = 0;
    4. int e = 10;
    5. while (d < 100)
    6. {
    7.     a++;
    8.     if (a >= e)
    9.     {
    10.         b++;
    11.         a -= e;
    12.     }
    13.     int c = (b * 10);
    14.     d = (c + a);
    15.    print (d);
    16. }
    Just a few notes to the piece of code. Depending on what variable values are actually needed inside the loop, this is just unnecessarily complicated and hard to read. Currently "a" goes from 1 to 9 in the first iteration and after that from 0 to 9. The value of d can be simplifed to
    d = b*10 +a;
    . The loop would run up to b==10 and a == 0 at which point the loop would terminate. Here's a .NET fiddle that shows the output of a, b and d.


    Things like that are usually done with nested for loops. Though since we don't know the exact purpose of this, we can't really say much about what you may want to do instead. The value of d at the point of the print statement would simply go from 1 to 100 (inclusive). If that's all you need, this would be simpler

    Code (CSharp):
    1. for(int i = 0; i < 100; i++)
    2. {
    3.    print(i+1)
    4. }
    The values of a and b are kinda strange since a is essentially offset by 1. Whenever possible you should stick to a 0 based counting system. This makes later calculations much easier.
     
    spiney199 likes this.