How to reference another entity?

So in an rts game i would have an attacker entity and an attacked entity. In this case the attacker would update the attacked’s hp component based on the attackers damage component. To do that i wanted to reference the attacked by saving it in a Entity IComponentData and then just do something like “Attacked.HP - Attacker.Damage”. However trying to make a “struct Entity : IComponentData” doesnt work with Entity so how should i instead reference the other entity to change its data?

You can try create an entity field inside the struct

Sorry if i wasn’t clear in my original post. I tried to add an entity field inside the struct but that pops up as an error. “ArgumentException: Attacked contains a field of Entity, which is neither primitive nor blittable.” Because i cant store entity’s as components i am wondering how to reference them in code.

I’m not aware of this not being possible. Weird, I just recreated this in my scene and it worked.

Hmm maybe im doing it wrong? Heres my code:

using Unity.Entities;

public struct Attacked : IComponentData
{
    public Entity Value;
}

which results in the following error for me.
Error

ArgumentException: Attacked contains a field of Entity, which is neither primitive nor blittable.
Unity.Entities.TypeManager.ThrowOnDisallowedComponentData (System.Type type, System.Type baseType, System.String baseTypeDesc) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/Types/TypeManager.cs:1169)
Unity.Entities.TypeManager.ThrowOnDisallowedComponentData (System.Type type, System.Type baseType, System.String baseTypeDesc) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/Types/TypeManager.cs:1174)
Unity.Entities.TypeManager.CheckIsAllowedAsComponentData (System.Type type, System.String baseTypeDesc) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/Types/TypeManager.cs:1122)
Unity.Entities.TypeManager.BuildComponentType (System.Type type, System.Int32[ ] writeGroups) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/Types/TypeManager.cs:1216)
Unity.Entities.TypeManager.BuildComponentType (System.Type type) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/Types/TypeManager.cs:1187)
Unity.Entities.TypeManager.AddAllComponentTypes (System.Type[ ] componentTypes, System.Int32 startTypeIndex, System.Collections.Generic.Dictionary2[TKey,TValue] writeGroupByType) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/Types/TypeManager.cs:1002) UnityEngine.Debug:LogException(Exception) Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/Stubs/Unity/Debug.cs:19) Unity.Entities.TypeManager:AddAllComponentTypes(Type[ ], Int32, Dictionary2) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/Types/TypeManager.cs:1025)
Unity.Entities.TypeManager:InitializeAllComponentTypes() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/Types/TypeManager.cs:964)
Unity.Entities.TypeManager:Initialize() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/Types/TypeManager.cs:464)
Unity.Entities.AttachToEntityClonerInjection:Initialize() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities.Hybrid/Injection/CompanionGameObject.cs:27)
Unity.Entities.AttachToEntityClonerInjection:.cctor() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities.Hybrid/Injection/CompanionGameObject.cs:21)
UnityEditor.EditorAssemblies:ProcessInitializeOnLoadAttributes(Type[ ])

That’s weird. I use Entity member variables inside component structs all the time.

Is it possible you have a type called Entity that is used instead of Unity.Entities.Entity. Simple way to check is explicitly use to full namespace type to avoid any possible conflicts.

2 Likes

Yep that was it, turns out i called a monobehaviour Entity before i started trying ECS. Thanks for the help everyone.