# How is RegisterGenericComponentType attribute used?

**URL:** https://discussions.unity.com/t/how-is-registergenericcomponenttype-attribute-used/745884
**Category:** Entity Component System (ECS)
**Tags:** Entities
**Created:** [June 4, 2019, 1:48pm UTC](https://discussions.unity.com/t/how-is-registergenericcomponenttype-attribute-used/745884 "2019-06-04T13:48:14Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![davenirline](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/davenirline/32/610603_2.png) [@davenirline](https://discussions.unity.com/u/davenirline)
#### Post date: [June 4, 2019, 1:48pm UTC](https://discussions.unity.com/t/how-is-registergenericcomponenttype-attribute-used/745884/1 "2019-06-04T13:48:14Z")

</div>

While upgrading, I got this error while playing the project:

ArgumentException: Unknown Type:`CommonEcs.CollectSharedComponentsSystem`1+Collected[CommonEcs.SpriteLayer]` All ComponentType must be known at compile time. For generic components, each concrete type must be registered with [RegisterGenericComponentType].

I don’t know how to use it. It says it can only be used in Assembly declarations. I tried setting as specified [here](https://docs.microsoft.com/en-us/dotnet/framework/app-domains/set-assembly-attributes) but it doesn’t work. Can somebody show code on how use this?

---

<div class="post-metadata">

### Author: ![davenirline](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/davenirline/32/610603_2.png) [@davenirline](https://discussions.unity.com/u/davenirline)
#### Post date: [June 4, 2019, 2:06pm UTC](https://discussions.unity.com/t/how-is-registergenericcomponenttype-attribute-used/745884/2 "2019-06-04T14:06:38Z")

</div>

I got it to working in compilation but it still showed the same error on play. I tried this:

```csharp
[assembly: RegisterGenericComponentType(typeof(SpriteLayerInstancesSystem))]

namespace CommonEcs {
    [UpdateInGroup(typeof(PresentationSystemGroup))]
    public class SpriteLayerInstancesSystem : CollectSharedComponentsSystem<SpriteLayer> {
    }
}

```

and this:

```csharp
[assembly: RegisterGenericComponentType(typeof(CollectSharedComponentsSystem<SpriteLayer>))]

namespace CommonEcs {
    [UpdateInGroup(typeof(PresentationSystemGroup))]
    public class SpriteLayerInstancesSystem : CollectSharedComponentsSystem<SpriteLayer> {
    }
}

```

I also tried setting it to the class mentioned in the error message. It still doesn’t work:

```csharp
[assembly: RegisterGenericComponentType(typeof(SpriteLayerInstancesSystem))]

namespace CommonEcs {
    public class AddGameObjectSpriteToLayerSystem : ComponentSystem {
        ...
    }

```

---

<div class="post-metadata">

### Author: ![GilCat](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/gilcat/32/436068_2.png) [@GilCat](https://discussions.unity.com/u/GilCat)
#### Post date: [June 4, 2019, 3:22pm UTC](https://discussions.unity.com/t/how-is-registergenericcomponenttype-attribute-used/745884/3 "2019-06-04T15:22:03Z")

</div>

Have a look at [this post](https://discussions.unity.com/t/744682/5)

---

<div class="post-metadata">

### Author: ![davenirline](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/davenirline/32/610603_2.png) [@davenirline](https://discussions.unity.com/u/davenirline)
#### Post date: [June 4, 2019, 3:59pm UTC](https://discussions.unity.com/t/how-is-registergenericcomponenttype-attribute-used/745884/4 "2019-06-04T15:59:06Z")

</div>

> [@GilCat](#):
>
> Have a look at [this post](https://discussions.unity.com/t/744682/5)

Thanks

---

<div class="post-metadata">

### Author: ![davenirline](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/davenirline/32/610603_2.png) [@davenirline](https://discussions.unity.com/u/davenirline)
#### Post date: [June 4, 2019, 4:19pm UTC](https://discussions.unity.com/t/how-is-registergenericcomponenttype-attribute-used/745884/5 "2019-06-04T16:19:56Z")

</div>

Still didn’t work. Where are you supposed to place AssemblyInfo.cs? Is it the same folder as the component with generic parameters?

Also tried deleting PackageCache and ScriptAssemblies. Still didn’t work. ☹ I hope a Unity dev can comment.

---

<div class="post-metadata">

### Author: ![GilCat](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/gilcat/32/436068_2.png) [@GilCat](https://discussions.unity.com/u/GilCat)
#### Post date: [June 4, 2019, 6:15pm UTC](https://discussions.unity.com/t/how-is-registergenericcomponenttype-attribute-used/745884/6 "2019-06-04T18:15:25Z")

</div>

You can put it anywhere in your project.  
I’m using that in my project and I have a few generics. They all work fine for me.

---

<div class="post-metadata">

### Author: ![GilCat](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/gilcat/32/436068_2.png) [@GilCat](https://discussions.unity.com/u/GilCat)
#### Post date: [June 4, 2019, 6:28pm UTC](https://discussions.unity.com/t/how-is-registergenericcomponenttype-attribute-used/745884/7 "2019-06-04T18:28:42Z")

</div>

Oh I see your generics are on the system and not on the component. That shouldn’t even require you to declare the generics.  
What is the definition for **CollectSharedComponentsSystem\<\>** and **SpriteLayer?**

---

<div class="post-metadata">

### Author: ![davenirline](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/davenirline/32/610603_2.png) [@davenirline](https://discussions.unity.com/u/davenirline)
#### Post date: [June 5, 2019, 7:03am UTC](https://discussions.unity.com/t/how-is-registergenericcomponenttype-attribute-used/745884/8 "2019-06-05T07:03:46Z")

</div>

Here

```csharp
public abstract class CollectSharedComponentsSystem<T> : ComponentSystem where T : struct, ISharedComponentData {
    ...
}

public struct SpriteLayer : ISharedComponentData, IEquatable<SpriteLayer> {
    ...
}

```

---

<div class="post-metadata">

### Author: ![GilCat](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/gilcat/32/436068_2.png) [@GilCat](https://discussions.unity.com/u/GilCat)
#### Post date: [June 5, 2019, 12:04pm UTC](https://discussions.unity.com/t/how-is-registergenericcomponenttype-attribute-used/745884/9 "2019-06-05T12:04:35Z")

</div>

That doesn’t require you to register generics. It must be something else that you are not showing.  
By looking at the error more carefully it points otherwise. It says that there is a component **Collected** and that is what must be registered.

---

<div class="post-metadata">

### Author: ![davenirline](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/davenirline/32/610603_2.png) [@davenirline](https://discussions.unity.com/u/davenirline)
#### Post date: [June 5, 2019, 2:03pm UTC](https://discussions.unity.com/t/how-is-registergenericcomponenttype-attribute-used/745884/10 "2019-06-05T14:03:44Z")

</div>

I think it’s this one:

```csharp
public abstract class CollectSharedComponentsSystem<T> : ComponentSystem where T : struct, ISharedComponentData {
        protected struct Collected : ISystemStateComponentData {
        }

    ...
}

```

Should I register it as:

```csharp
[assembly: RegisterGenericComponentType(typeof(CollectSharedComponentsSystem<SpriteLayer>.Collected))]

```

Edit:  
It worked. Thanks for the help!
