Search Unity

Small snippets of UnityScript

Discussion in 'Scripting' started by Ippokratis, Feb 12, 2012.

  1. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    CountChildren

    Input : a transform
    Output : Number of children all hierarchy levels down.
    Interesting : Use of recursion.

    Code (csharp):
    1.  
    2. function countChildren(tr : Transform):int
    3. {
    4.     var i : int  = 0;
    5.     var t : Transform;
    6.    
    7.     for ( t in tr )
    8.     {
    9.         i += countChildren(t) + 1;
    10.     }
    11.  
    12.     return i;
    13. }
    14.  
     
    Last edited: Feb 12, 2012
  2. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    How to make a custom class that exposes its fields in the inspector
    Code (csharp):
    1.  
    2. //Allows you to add this script to a GO, make sure you
    3. //name the script with the same name (e.g. in this example myDummyClass)
    4. public class myDummyClass extends MonoBehaviour
    5. {
    6.     //The above instance shows in the inspector
    7.     public var myClassInstance : myClass;
    8.     //Allows the inspector to see inside your custom class
    9.     public class myClass extends System.Object
    10.     {
    11.         public var nameField : String = "My Class Name field value";
    12.     }
    13. }
    14.  
     
  3. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Properties in js (Thanks again Neil - Mike )
    Code (csharp):
    1. public class myDummyClass extends MonoBehaviour
    2. {
    3.     public var myClassInstance : myClass;
    4.    
    5.     public class myClass extends System.Object
    6.     {
    7.         public var nameField : String = "My Class Name field value";
    8.         public var moneyField : int = 0;
    9.        
    10.          function get HasMoneyProperty():boolean
    11.         {
    12.             return moneyField > 0;
    13.         }
    14.     }
    15.    
    16.     function Start()
    17.     {
    18.         myClassInstance = new myClass();
    19.         myClassInstance.moneyField = 10;
    20.         Debug.Log(myClassInstance.moneyField.ToString());
    21.         Debug.Log(myClassInstance.HasMoneyProperty.ToString());
    22.         myClassInstance.moneyField = 0;
    23.         Debug.Log(myClassInstance.moneyField.ToString());
    24.         Debug.Log(myClassInstance.HasMoneyProperty.ToString());
    25.     }
    26. }
     
    Last edited: Sep 17, 2012
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Regarding the previous script, wouldn't you just do:

    Code (csharp):
    1. public class MyClass
    2. {
    3.     public var nameField : String = "My Class Name field value";
    4. }
    5.  
    6. var foo : MyClass;
    ?

    Regarding properties, I think this would work better:

    Code (csharp):
    1. public class MyClass
    2. {
    3.     public var nameField : String = "My Class Name field value";
    4.     public var moneyField : int = 5;
    5.     function get hasMoneyProperty() : boolean
    6.     {
    7.         return moneyField > 0;
    8.     }
    9. }
    10.  
    11. var foo : MyClass;
    Also note my bugfix in the getter. ;)

    --Eric
     
  5. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Thanks Eric :)
     
  6. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    So I tried doing properties in Unityscript quite some time ago, failed completely, and figured it was just impossible.
    Turns out you can only declare get functions inside a class.

    As in, you have to explicitly have a class:
    Code (csharp):
    1. class Mine extends MonoBehaviour {
    2.   function get property() : int { return 1; }
    3. }
    If you remove the class declaration, suddenly 'get' is an unexpected token (even though... I'm declaring a class by simply having the script exist).