Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

gameObject.GetComponents() not working

Discussion in 'Scripting' started by hannes-dev, Sep 3, 2012.

  1. hannes-dev

    hannes-dev

    Joined:
    Apr 27, 2012
    Posts:
    132
    var skillList : skills[];

    print("the array " + gameObject.GetComponents(skills));
    skillList = gameObject.GetComponents(skills) as skills[];
    print(" the array in skillList" +skillList);

    the first prints the array i want
    the second puts the array in var skilllist
    the third line prints skilllist, should be same result as first line, but instead prints "the array in skillList" +null / nothing
     
    Last edited: Sep 3, 2012
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    gameObject. is useless in that code.
    as skills[] is useless in JavaScript.
    skills is a bad name for a class.
    Somebody else who understands UnityScript will come along and tell you what magic is going wrong, but understand your code before you try to understand that magic.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I understand Unityscript, but unfortunately I don't really understand what you're saying or trying to do.

    --Eric
     
  4. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    I think he tries to convert the Component[] to skills[], not sure how this would be done in UnityScript, but here is an easy way without any dependencies needed:

    Code (csharp):
    1.  
    2. var skillList : skills[];
    3. var components : Component[];
    4.  
    5. components = GetComponents(skills);
    6.  
    7. print("the array " + components);
    8.  
    9. skillList = new skills[components.Length];
    10. for (var i = 0; i < components.Length; ++i)
    11.     skillList[i] = components[i] as skills;
    12.    
    13. print(" the array in skillList" +skillList);
    14.  
    I am not that fit in UnityScript but I guess this is what he wants in some kind.

    Jessy is also correct with what he said. gameObject is not needed when working in a component/script context, as GetComponent and many other methods are too available in your script.
     
  5. hannes-dev

    hannes-dev

    Joined:
    Apr 27, 2012
    Posts:
    132
    (i know gameObject wasn't necessary, but still tried it, same with transform etc
    you never know)

    i found the solution
    i had to do gameObject.GetComponents.<skills>();
    instead of gameObject.GetComponents(skills));

    i thought it returned Object[] wich i could cast to skills[]
    but instead it returned Component[] like said

    @jessy, why is " as skills[] " useless in unityscript?
    and why is skills a bad name

    @mark, you that was what i was trying to do

    thx for the help everyone