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

Bug Input.GetKey in a Property crashes Unity

Discussion in 'Editor & General Support' started by Pacrombie, Mar 10, 2023.

  1. Pacrombie

    Pacrombie

    Joined:
    Sep 8, 2018
    Posts:
    4
    In the following code, Unity instantly crashes every time I press the Left Shift key. Is this intentional? Do I just have to use a flag set on Update to work around this?

    Code (CSharp):
    1.     public float MoveSpeed
    2.     {
    3.         get
    4.         {
    5.             float moveSpeed =
    6.                 baseMoveSpeed
    7.                 + (Input.GetKey(KeyCode.LeftShift) ? SprintSpeedAddition : 0)
    8.                 + (dexterityMoveSpeedModifier * Mathf.Pow(GameManager.Instance.CurrentCharacter.Dexterity, 1 / 3));
    9.             // TODO: math including dexterity, equipment, etc.
    10.  
    11.  
    12.             return moveSpeed;
    13.         }
    14.     }
    Other than the Shift thing, it does what I want it to do.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,016
    Define crash. Like an actual crash to desktop? Or does some other exception throw?
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,905
    Does SprintSpeedAddition call MoveSpeed, even indirectly?
     
  4. Pacrombie

    Pacrombie

    Joined:
    Sep 8, 2018
    Posts:
    4
    Sorry for leaving this. I figured it out earlier: it wasn’t the input crashing it, it was SprintSpeedAddition calling MoveSpeed, so you were right, halley. Silly mistake on my part but thank you for your responses :)
     
  5. Pacrombie

    Pacrombie

    Joined:
    Sep 8, 2018
    Posts:
    4
    It was a full crash to desktop. That’s why I was so shocked and confused lol
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    You want to seriously do yourself a favor and avoid code like the above.

    Do it one computation per line, it will result in the same ultimate output.

    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - StarManta on the Unity3D forums
     
  7. Pacrombie

    Pacrombie

    Joined:
    Sep 8, 2018
    Posts:
    4
    I absolutely agree, I’ve just started this project a few days ago and I definitely have some revision to do.