I don’t really see how it could get any better than what you’ve already posted; one creates an object and then passes values to it.
If by better, you mean in one line ( not always better) , then you might consider creating a static method for creating instances of that class.
public class Weapon : MonoBehaviour{
public static Weapon CreateWeapon(string name, int dmg){
GameObject g = new GameObject(name);
Weapon result = g.AddComponent<Weapon>();
result.damage = dmg;
return result;
}
private int damage;
}
So your creation code would look like:
Weapon a = Weapon.CreateWeapon("Knife",5);
I assume here that you’re not actually interested in calling the initialize-method on the GameObject, but the created MonoBehaviour… If that is not the case, then you’ll need an extension method for GameObject for which you can define as many parameters as required.