Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Return an Entity from a scene

Discussion in 'Project Tiny' started by sniffle63, Jun 19, 2019.

  1. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    365
    Is there a way to return an entity from a scene that was just created?

    Say i spawned a scene in, and wanted a reference to the only entity in that scene. What would be the best way of doing that

    At the moment the only thing i can think of is to add a "LoadScene" custom component to the entity in the scenes and doing a foreach looking for the component and performing actions then removing the component.

    But that just seems like the biggest work around ever.

    In the previous version we could retrieve Entities from the EntityGroup after instantiating the EntityGroup, but i cant seem to find a way to do that in this version. I could of just overlooked it of course

    And/or can i retrieve entities only from a certain scene?
    brave_2019-06-19_13-37-05.png
    i found this in the documents, but cant seem to figure out how to actually access it
     
    Last edited: Jun 19, 2019
  2. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    365
    this is how i ended up doing it

    Code (CSharp):
    1.  
    2.                 SceneService.LoadSceneAsync(snakeTailToSpawn);
    3.              
    4.                 Entities.ForEach((Entity entity, ref SnakeTail tail) =>
    5.                 {
    6.                     if (!tail.isCreated)
    7.                     {
    8.                         tail.isCreated = true;
    9.                         Entities.ForEach((DynamicBuffer<SnakeSegment> segments) =>
    10.                         {
    11.                             segments.Add(new SnakeSegment
    12.                             {
    13.                                 Reference = entity
    14.                             });
    15.                         });
    16.                     }
    17.                 });
     
  3. Dunk86

    Dunk86

    Joined:
    May 10, 2015
    Posts:
    53
    So you have the polling and initialisation code straight after the LoadSceneAsync call and it always find the new object? Why is it named Async then? It would be great (but weird) if this works!

    EDIT: Nope, just getting my hopes up :)
     
    Last edited: Jun 21, 2019
  4. AlexMasse

    AlexMasse

    Joined:
    Jun 29, 2014
    Posts:
    19
    The ForEach query won't find the loaded entity straight after but if this code is in an OnUpdate() method, it will try to find it every frame so it should work.
     
  5. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    365
    Its in the onUpdate method, so it keeps searching untill it finds it, the loadSceneAsync is done somewhere else, i just put it above the ForEach for reference