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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to get Entity's name in code

Discussion in 'Project Tiny' started by ScrapCupcake, May 12, 2019.

  1. ScrapCupcake

    ScrapCupcake

    Joined:
    Mar 10, 2014
    Posts:
    3
    So, say I've got an entity I'm world.forEach over; how would I get it's name from the editor to log or display or use as a key?
     
  2. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    129
    You could do
    this.world.getEntityName(yourEntity)
    which returns the entity's name as seen in the editor.

    You could also get the name from the EntityInformation component on your entity, but I haven't seen anyone really use that method so I guess it's not as mainstream to do it that way.
     
    NotaNaN, reallyhexln and ScrapCupcake like this.
  3. TheSmokingGnu

    TheSmokingGnu

    Joined:
    May 1, 2017
    Posts:
    22
    Hi! Does anyone know how to do this in 0.13.0 preview 24? Looks like the this.world.getEntityName(yourEntity) is missing

    Searching the Entities sources I have found only the way to do this in editor (Unity.Entities\EntityComponentStore.cs):
    upload_2020-7-24_20-52-41.png
     
  4. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    324
    Hue hue! I know how to do this!

    Have you tried the EntityManager?
    Code (CSharp):
    1. EntityManager.GetName(entity);
    2. EntityManager.SetName(entity, "Bob the Entity");
    The only caveat is that you will need an instance of an EntityManager to do this (obviously). But as long as you have access to one, you should be good!
     
    Last edited: Jul 25, 2020
    oAzuehT, ben-rasooli, mikaelK and 2 others like this.
  5. mikaelK

    mikaelK

    Joined:
    Oct 2, 2013
    Posts:
    281
    @GliderGuy
    Works fine so thanks, but when I build the project I get

    "Assets\Plugins\MyPlugin\TargetingIndicator.cs(32,82): error CS1061: 'EntityManager' does not contain a definition for 'GetName' and no accessible extension method 'GetName' accepting a first argument of type 'EntityManager' could be found (are you missing a using directive or an assembly reference?)"

    Is this editor only or why do I get this? I only get these kind of warnings with editor scripts that somehow end up in the build.

    Could this be because I'm calling this from a monobehavior?

    Edit: No I think its editor only. So need to figure out a way to get name of entity that support building a project.

    Code (CSharp):
    1.                 World.DefaultGameObjectInjectionWorld.EntityManager.GetName(target.entity);
     
    Last edited: Sep 17, 2020
  6. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    324
    Yeah, unfortunately it's editor-only.
    In fact, I think Entity names are entirely editor-only (the names are not recorded nor exist at runtime).
    So, unless something has changed recently, you won't be able to use them as far as I'm aware.

    BUT!
    What you could do is create a NameComponent, and store your Entity's name within it in the form of a FixedString (16, 32, 64, 256, etc).
    Then, OnValidate(), you can update a "nameString" on your AuthoringComponent to the name of your GameObject, and then on GameObjectToEntityConversion use the "nameString" to set the FixedString in your NameComponent.
     
    Last edited: Sep 17, 2020
    oAzuehT and mikaelK like this.
  7. mikaelK

    mikaelK

    Joined:
    Oct 2, 2013
    Posts:
    281
    @rupt
    Yeah thanks.
    There is a similar post with example code.
    https://forum.unity.com/threads/is-there-a-name-component-for-entities.801732/
     
    oAzuehT and NotaNaN like this.