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

Is there a way to shorten these three lines?

Discussion in 'Scripting' started by qwertyuiop11110, Apr 13, 2020.

  1. qwertyuiop11110

    qwertyuiop11110

    Joined:
    Mar 16, 2020
    Posts:
    24
    Code (CSharp):
    1. float outcome = 0.0f;
    2.  
    3. if (boolOne) outcome = 1.0f;
    4. else if (boolTwo) outcome = -1.0f;
    I feel like there's a way to shorten these three lines without losing its readability.
    PS: No, it's not GetAxis.
     
  2. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    161
    Code (CSharp):
    1. float outcome = boolOne ? 1f : (boolTwo ? -1f : 0f);
     
    qwertyuiop11110 likes this.
  3. qwertyuiop11110

    qwertyuiop11110

    Joined:
    Mar 16, 2020
    Posts:
    24
    I knew it had to be some ternary operator that I couldn't discover, thanks.