Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Get entity from aspect

Discussion in 'Entity Component System' started by Smellfish, Feb 8, 2023.

  1. Smellfish

    Smellfish

    Joined:
    Feb 21, 2020
    Posts:
    8
    Hello everyone!

    Sorry if it's a noob question, but how can i get the entity if i already have one of it's aspects?
    I've been searching the web, and haven't found anything yet.

    Thanks in advance!
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    It is unclear to me, what you are asking for.
    You can get entity from the same job, you get aspect.
     
  3. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
  4. DaxodeUnity

    DaxodeUnity

    Unity Technologies

    Joined:
    Aug 5, 2021
    Posts:
    27
    Well, there's a few ways to get an entity from an aspect. 1. is doing it while iterating, so if you're using SystemAPI.Query you can use .WithEntityAccess and if you're using IJobEntity you simply need one parameter to be an entity.

    That said there's also option 2, if a core part of your aspect needs entity access, then it might make more sense to have your aspect get the entity, which you can do by having a field in your aspect be of type Entity. Just like RefRO and similiar in an aspect, it will know to assign the property when getting the aspect.

    Code (CSharp):
    1. readonly partial MyAspect : IAspect {
    2.     readonly Entity myEntity;
    3.     // etc ...
    4. }