✅ Pass in arguments to a generic constructor
i want to be able to create new instances of
T
in the method, but i get the error 'T': cannot provide arguments when creating an instance of a variable type
. how can i make this work? one precondition is that all child of Component
will only have one argument in their constructor6 Replies
the short answer is you can't. The long answer is you either need to use reflection or an initialize pattern like
i found this solution too
yup, that's reflection
it's not free, so it depends on your needs
damn that really sucks
i want to make a game object system like unity does
So you're wanting something like
AddComponent
where it sets the gameObject
property?
you would be able to do that if you did something like
i get the error
'TransformComponent' cannot satisfy the 'new()' constraint on parameter 'T' in the generic type or or method 'GameObject.AddComponent<T>()' because 'TransformComponent' has required members.
its because of the required
keyword
i fixed it by removing the keyword and making the Host nullable (which to me is not the best solution)