Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Find an object in subscene

Discussion in 'Entity Component System' started by Deleted User, Aug 17, 2019.

  1. Deleted User

    Deleted User

    Guest

    How can I find an object with name in a subscene?
    I have a logic scene, and I put every game level inside a subscene. Now I need to find the player's spawn point in that particular level which has been named "Player Spawn Point".
    But when GameObject.Find returns null.
     
  2. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    I haven't been able to use SubScenes yet so I may be unhelpful, but are they already entities in the debugger? There is EntityManager.GetAllEntities() and Get/SetName() so you can find it that way, alternatively you might make it a Singleton entity or have PlayerSpawnPoint as a tag component.
     
  3. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,697
  4. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
    There are no gameobjects in a subscene, just entities (Except edit mode). You can tag your spawn points with a component:

    Code (CSharp):
    1. struct PlayerSpawnPoint : IComponentData {}
    You get the spawnpoints with a query like

    Code (CSharp):
    1. Entities.Foreach((ref PlayerSpawnPoint playerSpawnPoint) => {});
    or an EntityQuery or a IJobForEach... depends on your specific needs.
     
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    Get/SetName only with in editor (you need to wrap them with #if unity_editor else you'll get errors when building)

    So this is not the first time this question has come up and I feel like there is a disconnect sometimes with how Unity developed subscenes and how the community is trying to use them.

    So to me, subscenes are designed as a way of taking what was a single large scene and breaking it up into parts for the purpose of streaming them for optimization. If used like this there is probably never a reason you should need to know what subscene an entity is part of, because the entity is not part of a subscene they only ever part of the parent scene. There is only 1 scene.

    What you appear to be doing is that your subscenes are designed to be completely separate scenes. So why not just treat it like that?

    There's nothing stopping you just loading your scene with SceneManager.LoadScene[Async]. All the entities that need converting will still convert.
     
    RecursiveEclipse likes this.
  6. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
    Each entity loaded from a subscene is referenced by it, so it's a part of it. Otherwise streaming wouldn't work :)

    With a subscene nothing has to be converted as this is done during design time (When a subscene is saved).

    I agree, but they can be used for other purposes, too. For example i use them to store different levels, the only drawback is that you can't attach data to a subscene yet, e.g. a level number. For each level number i have a reference to the subscene monobehaviour and compare it to each subscene (they keep their monobahviour reference) in a ComponentSystem to find the right on to load.

    Yep, this approach will be easier to start with!
     
  7. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    Was more a figure of speech that didn't come across well than an actual fact.

    Oh there are definitely performance benefits, but when level changing a load screen is really not a big deal. A lot of the time even if loading was instantaneous you'd still want some type of load screen to make the transition smoother.

    Not saying they aren't good for other uses, just saying that is does not seem to be how they were first designed. I definitely would not be surprised if Unity develops them into a more universal solution and they become much more suited for this purpose.
     
    daschatten likes this.
  8. Deleted User

    Deleted User

    Guest

    Do I need to put ConvertToEntity script on it too?
    I have created an author monobehaviour that attaches PlayerSpawnPoint component to its entity. But I couldn't find it with queries.
    When I attach ConvertToEntity to my gameobject, it starts working. But it gives me an error in inspector since the gameobject is part of a subscene and has ConvertToEntity on it too.
    Is it ok to have ConvertToEntity component on a gameobject that is part of a subscene?
     
  9. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
    You get that error in the inspector view, but it works. Though you only need it if you don't use subscenes. In a subscene all entities get converted automatically. Click "Rebuild Entity Cache" on the subcene and see if it works without ConvertToEntity.