Search Unity

can you use AddComponent with a custom script?

Discussion in 'Scripting' started by petey, Apr 11, 2011.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hi there,

    Just wondering, is ther a way to use AddComponent to add a custom script to an object? I'm stuck trying to get it to work. I keep getting -
    No appropriate version of 'UnityEngine.GameObject.AddComponent
    I guess that makes sense because it is my own script but I figured sice you can do it in the editor window , there must be a way to do it in the game.

    Any Ideas?
    Thanks
    Pete
     
  2. Satoh

    Satoh

    Joined:
    Feb 17, 2011
    Posts:
    56
    1: Show me the code you used.
    2: try this:

    Code (csharp):
    1.  yourObjectName.AddComponent("YourScriptNameWithOutFileExtension");
    for another example...

    floorCube.AddComponent("FloorIsMadeOfLava");
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It should be gameObject.AddComponent, not GameObject (you're adding to a specific game object, not the class).

    --Eric
     
  4. aoakenfo

    aoakenfo

    Joined:
    Jul 14, 2009
    Posts:
    35
    Make sure you inherit from MonoBehavior.
     
  5. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I realize the docs show the string version first. You should read from the bottom of the page, instead. ;) GetComponent, at least, tips you off. And don't forget the new generic version, if you need to store the component in a variable.
     
    Last edited: Apr 11, 2011
  6. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Ahh Cool, thanks guys, I wasn't using the quotation marks :)
    Sorry, I don't know ho i missed that... embarrassing...
     
  7. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Like I said, you shouldn't.
     
  8. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hmm, I'm not sure I get it, how would you use this? GameObject.AddComponent.<T>
    Also, GetComponent is only for getting existing components, right?
     
  9. Satoh

    Satoh

    Joined:
    Feb 17, 2011
    Posts:
    56
    I think it's like this...

    GameObject.AddComponent.<YourScript>();

    But honestly I've always loathed typecasted functions like this... they never make sense to me.
     
  10. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I have no idea how it works in UnityScript. You could have made it yourself, in C#, before it was in the API, with an extension method (this still works, but is pointless):

    Code (csharp):
    1. public static T GenericAddComponent <T> (this GameObject gameObject) where T : Component {
    2.     return gameObject.AddComponent(typeof(T)) as T;
    3. }
    And then you could use it like this, exactly the same as AddComponent as it exists now:
    Code (csharp):
    1. BoxCollider boxCollider = gameObject.GenericAddComponent<BoxCollider>();
    Here's a great intro to extension methods:
    http://www.youtube.com/watch?v=va556bGnXIg
     
    Last edited: Apr 11, 2011