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

Boolean abracadabra

Discussion in 'Scripting' started by ThomasUnity, Dec 2, 2015.

  1. ThomasUnity

    ThomasUnity

    Joined:
    Jul 10, 2015
    Posts:
    17
    Hi,
    I am trying to convert some script from a course from C# to UnityScript, but i am stuck. It's a script that will test if i press the left or right arrow key.
    The main thing I don't understand in this C# script is the boolean. I always thought a boolean is only true or false. So what does this mean? Can the boolean have the values 'val < offValue' and 'val > offValue' ???

    I hope somebody can explain what is happening in this script, so i can translate it toy UnityScript.

    There is an enum called 'Condition' with 2 values: 'GreaterThan' and 'LessThan'.
    This is the piece of the script that I don't understand:

    Code (csharp):
    1.  
    2.  
    3.     public bool value{
    4.         get {
    5.             var val = Input.GetAxis(axisName);
    6.            
    7.             switch(condition){
    8.             case Condition.GreaterThan:
    9.                 return val > offValue;
    10.             case Condition.LessThan:
    11.                 return val < offValue;
    12.             }
    13.            
    14.             return false;
    15.         }
    16.    
    17.     }
    18.  
    19.  
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    It's asking a question, the result will be true or false. You can evaluate big groups of information or conditions as boolean.

    Simple example

    Code (csharp):
    1. if (firstValue > secondValue) // is the first number bigger than the second number?
    2.   {
    3.     DoBlue();
    4.   }
    5. else
    6.   {
    7.     DoRed();
    8.   }
    Whackier example

    Code (csharp):
    1. if (someMesh.material == referenceMaterial && firstValue > secondValue && !lookingForBlue) // combine lots of conditions that can be queried as true or false statements
    2.   {
    3.     DoBlue();
    4.   }
    In your specific case its saying "if the condition is "Greater Than", check if "val" is bigger than "offValue", then return true if it is or false if it is not.

    When you have `return (something)` you can shovel any condition you want in there. Since you're returning a bool just pile in whatever conditions you want that will result in a bool and it will return it.

    More practical examples

    Code (csharp):
    1. return (firstValue > secondValue); // is the firstValue bigger than the secondValue?
    2. return (someMaterial == someOtherMaterial); // are these materials the same?
    3. return (Application.loadedLevel == "FirstLevel"); // is this level called "FirstLevel"?
    4. return (enemyCount > 50); // are there more than 50 enemies?
    5. return (firstValue > secondValue) && (firstValue < secondValue); // will always be false
     
    Last edited: Dec 2, 2015
  3. ThomasUnity

    ThomasUnity

    Joined:
    Jul 10, 2015
    Posts:
    17
    So, if i understand it right:
    This: " return val > offValue; "
    Means: " return if val is greater than offValue" ?
     
  4. ThomasUnity

    ThomasUnity

    Joined:
    Jul 10, 2015
    Posts:
    17
    And in what line will the boolean be set to True?
     
  5. ThomasUnity

    ThomasUnity

    Joined:
    Jul 10, 2015
    Posts:
    17
    Super! Thanks a lot! I have learned something new! The statement "a > b" or whatever, is true or false, and that is what wil be returned!
     
    LaneFox likes this.
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Right, so `public bool value` is constantly being re-evaluated when something asks what it is. Your `get` here is dynamic in the sense that when something asks for it, you just calculate it and send the result back.
     
  7. ThomasUnity

    ThomasUnity

    Joined:
    Jul 10, 2015
    Posts:
    17
    Yeah, that's another small problem. UnityScript doesn't have Get, so I think I'll have to use the Update function to keep the boolean being re-evaluated.
     
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Well that sucks. I would really recommend learning C# as there are significantly more materials available for it and its much more widely used.

    FYI this is called a Property.
     
  9. ThomasUnity

    ThomasUnity

    Joined:
    Jul 10, 2015
    Posts:
    17
    I think you're right, the thought had occurred to me. But it seems much harder to learn then JavaScript or UnityScript. And I don't consider myself a real talent. Or is it equally difficult?
     
  10. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Its definitely not harder to learn C#. The bulk of reference scripts and information on the forums/answers/google/etc.. in C# is worth it over UnityScript IMO.
     
  11. ThomasUnity

    ThomasUnity

    Joined:
    Jul 10, 2015
    Posts:
    17
    you convinced me! I'll start tomorrow. :)
    And thanks again for your help.