Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

How do you simplify this script

Discussion in 'Scripting' started by VLADIMIRALI, Jan 12, 2018.

  1. VLADIMIRALI

    VLADIMIRALI

    Joined:
    Oct 21, 2017
    Posts:
    2
    Is there any way to simplify this script???

    if (x[0].istrue == false)
    {
    if (x[1].istrue == false)
    {
    if (x[2].istrue == false)
    {

    }
    }
    }
    else
    {

    }
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    22,245
    Collating results via a for loop or magic method would not yield any simplification unless you're lying and there's more to it.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,822
    Right now you have nested ifs.

    While you can shorten the ifs to

    if(!x[0].istrue) for example, beyond that there is no context in what is suppose to happen.

    You might be able to do a loop still, where you set a bool to false before the loop, loop through and if you hit true, set the bool to true and break, then handle whatever based on that bool, but again, with nested if statements and only this much info, it is difficult to say.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    5,801
    There is no way to improve what you posted without more information on the goal of the code.
     
  5. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    102
    I suppose you meant more than 3 variable comparisons...in that case, you could use a for loop but really depends on what you have to do.
     
    TaleOf4Gamers likes this.
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    5,801
    Technically it does nothing, so you could simplify it by deleting it.
     
  7. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,013
    Code (CSharp):
    1. //your current code
    2. if (x[0].istrue == false)
    3. {
    4.     if (x[1].istrue == false)
    5.     {
    6.         if (x[2].istrue == false)
    7.         {
    8.             Debug.Log("x[2] is false");
    9.             //do some code here
    10.         }
    11.     }
    12. }
    13. else
    14. {
    15.     Debug.Log("x[0] is true");
    16.     //do some code here
    17. }
    18.  
    19.  
    20. //no one knows what you're after, so here is my 2 cents.
    21. for(int i = 0; i < x.Length; i++)
    22. {
    23.     if(!x[i])
    24.     {
    25.         Debug.Log("x[" + i + "] is false");
    26.     }
    27. }
     
    VLADIMIRALI likes this.
  8. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    851
    I suppose one way to simplify is to use (!x[0].istrue) instead of (x[0].istrue == false).
     
  9. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    2,795
    If you want it shortened to ridiculousness, there's always this:

    Code (CSharp):
    1.         if (!(x[0].istrue | x[1].istrue | x[2].istrue))
    2.         {
    3.             Debug.Log("All false!");
    4.         }
    5.  
     
    ihgyug likes this.
  10. Mordus

    Mordus

    Joined:
    Jun 18, 2015
    Posts:
    104
    I'll see your ridiculousness and raise you:

    Code (CSharp):
    1. Debug.Log(((x[0].istrue | x[1].istrue | x[2].istrue) ? "Not a" : "A") + "ll false!");
     
    ihgyug likes this.