I´m currently building a modular system for a game in Unity. My problem is the following:
I have multiple classes, which are a sub type of parent. These classes are used in a prefab and have a sprite.
Now I want to have a type which has the functionality of parent and this has also to override some functions. Furthermore I still want to keep the Sprites. So my Idea was to simply make a generic with (where T : parent) and inherit from it. But this doesn’t work.
Does anyone know how I can manage to use the sub types but am still able to override functions from them?
Please paste the code you tried that doesn’t work, and also explain what “doesn’t work” about it - compile error? Because this should be a thing you can do.
class c:T where T : parent{}
You’ve got an extra “:T” in there.
public class c<T> where T:parent {
It is not possible to derive from a generic type.
https://www.microsoft.com/en-us/download/confirmation.aspx?id=7029
See section 4.4.3
No I haven´t. It was on purpose. I want to also DERIVE from this class
You can’t derive from T but you could do something like this:
public class Foo {}
public class Foo<T> : Foo where T : Foo {}
public class Bar<T> : Foo<T> {}
So you end up with a “Bar” that inherits from Foo and which also has Foo as its “T”, which should probably work for what you’re trying to do.