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

[Help]How to disable and enable the Entity?

Discussion in 'Project Tiny' started by WellC, Dec 24, 2018.

  1. WellC

    WellC

    Joined:
    Mar 2, 2015
    Posts:
    48
    When the character eats gold coins, the coins disappear.
    DONT DESTORY ENTITY! JUST LOOK LIKES SETACTIVE TRUE OR FALSE.
     
  2. WellC

    WellC

    Joined:
    Mar 2, 2015
    Posts:
    48
    Code (CSharp):
    1.         static setEntityEnabled(world: ut.World, entity: ut.Entity, enabled: boolean) {
    2.             let hasDisabledComponent = world.hasComponent(entity, ut.Disabled);
    3.             if (enabled && hasDisabledComponent) {
    4.                 world.removeComponent(entity, ut.Disabled);
    5.             }
    6.             else if (!enabled && !hasDisabledComponent) {
    7.                 world.addComponent(entity, ut.Disabled);
    8.             }
    9.         }
    This is working!
     
  3. actislav

    actislav

    Joined:
    Mar 2, 2018
    Posts:
    8
    hello, why i can not iterate through parent entity when children have ut.Disabled component? with such code:
    Code (JavaScript):
    1. let childCount = ut.Core2D.TransformService.countChildren(world, entity);
    2. for (; childCount > 0; childCount--) {
    3. let child  = ut.Core2D.TransformService.getChild(world, entity, childCount-1);
    4. }
    , i can disable children but can not enable them after, is it bug or normal, and pls make possible to use dynamic array of dynamic array of EntityReference, serialization failed in that case, or just make it impossible to create in editor...
     
  4. menkaix

    menkaix

    Joined:
    Jan 8, 2014
    Posts:
    2
    Hello,
    How do you get the entity from the system ?
     
  5. WellC

    WellC

    Joined:
    Mar 2, 2015
    Posts:
    48
    Yes, U need remove
    ut.Disabled
    first!
     
  6. WellC

    WellC

    Joined:
    Mar 2, 2015
    Posts:
    48
    Like this:
    Code (CSharp):
    1.  OnUpdate(): void {
    2.             let tmp_GameState = this.world.getConfigData(game.GameConfig);
    3.             if (tmp_GameState.State != game.GameState.Play) return;
    4.             this.world.forEach([ut.Core2D.TransformLocalPosition, game.Scroller], (transformPosition, scroller) => {
    5.                 let posistion = transformPosition.position;
    6.                 posistion.y -= scroller.speed;
    7.                 transformPosition.position = posistion;
    8.             });
    9.         }
     
  7. aeldron

    aeldron

    Joined:
    Feb 12, 2013
    Posts:
    32
    I think once you disabled an entity your world.forEach won't find them any more. You should instead use an EntityQuery and explicitly querying for the 'Disabled' component.
     
  8. Zionisias

    Zionisias

    Joined:
    Apr 23, 2019
    Posts:
    40
    Entities with a ut.Disabled component will indeed not show up in a world.forEach if you have not included ut.Disabled in the filter.

    If you include it, you will loop over all disabled entities with the other matching components:
    Code (CSharp):
    1. this.world.forEach([ut.Entity, game.ExampleComponent, ut.Disabled], (entity, exampleComp) => {
    2.        //Code to be executed on every disabled entity which has game.ExampleComponent.
    3. });
     
    Rupture13 likes this.