Search Unity

Initialising an array with it's subclass changes the arrays type?

Discussion in 'Scripting' started by Stickworm, Nov 29, 2012.

  1. Stickworm

    Stickworm

    Joined:
    Apr 24, 2011
    Posts:
    67
    Hi I was very confused for a while as to why my code wasn't behaving as expected, is this a bug or do I somehow need to be more explicit in the array definition? I would have thought the returned type should be BaseClass[] not SubClass[]? Cheers

    Code (csharp):
    1. class BaseClass{
    2.     function BaseClass(){
    3.     }
    4. }
    5.  
    6. class SubClass extends BaseClass{
    7.     function SubClass(){
    8.     }
    9. }
    10.  
    11. var testArray: BaseClass[] = [new SubClass()];
    12.  
    13. function Start(){
    14.     Debug.Log(testArray.GetType());
    15. }