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.
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.
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):
public static T GenericAddComponent <T> (this GameObject gameObject) where T : Component {
return gameObject.AddComponent(typeof(T)) as T;
}
And then you could use it like this, exactly the same as AddComponent as it exists now: