Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Using GetComponent in C#...

Discussion in 'Scripting' started by jeremyace, Apr 22, 2006.

  1. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Hey guys, I am having issues using GetComponent in C#

    Code (csharp):
    1.  
    2. //temp is a gameobject I create earlier
    3.  
    4. Collider col = temp.GetComponent(Collider);
    5.  
    The error that line throws is: Expression denotes a "type" where a "variable" or "method group" was expected.

    I am using it just as it said in the docs...so what am I missing?

    Thanks guys,
    -Jeremy
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Argh... I ended up finding this in some code on the wiki:
    Code (csharp):
    1.  
    2. temp.GetComponent(typeof(Collider)) as Collider;
    3.  
    I would really think this usage sould be on the docs somewhere as it just says: GetComponent(Collider) in the docs. But maybe it's just me... :eek:

    -Jeremy
     
  3. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    It's not you or the docs, it is C#.
     
  4. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Yeah, I do understand that, but it wasn't clear that you even had to do that to pass what the unity function needed.

    -Jeremy
     
  5. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Heh, on a related note, anyone know a good C# book?

    Thanks,
    -Jeremy
     
  6. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    I liked this one.
     
  7. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Thanks Neil, I will check it out.

    -Jeremy