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

Constructors and lists in JS

Discussion in 'Scripting' started by basementApe, Nov 11, 2015.

  1. basementApe

    basementApe

    Joined:
    Oct 2, 2013
    Posts:
    19
    Hello.

    I'm in the process of converting Sebastian Lague's excellent tutorial series from C# to Javascript, and things have been going well right up until the middle of episode 7 where he's adding to a list using a constructor.

    Sebastian's C# version:
    Code (csharp):
    1.  
    2. struct PassengerMovement {
    3. public Transform transform;
    4. public Vector3 velocity;
    5. public bool standingOnPlatform;
    6. public bool moveBeforePlatform;
    7. public PassengerMovement(Transform _transform, Vector3 _velocity, bool _standingOnPlatform, bool _moveBeforePlatform) {
    8. transform = _transform;
    9. velocity = _velocity;
    10. standingOnPlatform = _standingOnPlatform;
    11. moveBeforePlatform = _moveBeforePlatform;
    12. }
    13. }
    14.  
    My Javascript conversion:
    Code (csharp):
    1.  
    2.     // class extends System.ValueType substitutes struct for javascript
    3.     public class PassengerMovement extends System.ValueType
    4.     {
    5.         public var transform:Transform;
    6.         public var velocity:Vector3;
    7.         public var standingOnPlatform:boolean;
    8.         public var moveBeforePlatform:boolean;
    9.        
    10.         public function PassengerMovement( _transform:Transform, _velocity:Vector3, _standingOnPlatform:boolean, _moveBeforePlatform:boolean)
    11.         {
    12.             transform = _transform;
    13.             velocity = _velocity;
    14.             standingOnPlatform = _standingOnPlatform;
    15.             moveBeforePlatform = _moveBeforePlatform;
    16.         }
    17.     }
    18.  
    The list declaration:

    C#:
    Code (csharp):
    1.  
    2. List<PassengerMovement> passengerMovement;
    3.  
    Javascript:
    Code (csharp):
    1.  
    2. var passengerMovement:List.<PassengerMovement>;
    3.  
    And trying to add to the list like so:

    C#:
    Code (csharp):
    1.  
    2. passengerMovement.Add(new PassengerMovement(hit.transform,new Vector3(pushX,pushY), directionY == 1, true));
    3.  
    Javascript:
    Code (csharp):
    1.  
    2. passengerMovement.Add(new passengerMovement(hit.transform, new Vector3(pushX, pushY), directionY == 1, true));
    3.  
    ... and this last bit is where it breaks down. I get an error from the editor saying "It is not possible to invoke an expression of type System.Collections.Generic.List.<PlatformController.passengerMovement>".

    So I guess Javascript doesn't like it when you try to add constructors to a list or some such? I'm still pretty new to Unity scripting so I'm not sure why this happens or how to circumvent it. It would be great if anyone could help out with this.
     
    Last edited: Nov 11, 2015
  2. basementApe

    basementApe

    Joined:
    Oct 2, 2013
    Posts:
    19
    Ah nevermind, case-sensitive got me :p