Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

How i can disable entity with child objects?

Discussion in 'Project Tiny' started by TheFordeD, Feb 6, 2019.

  1. TheFordeD

    TheFordeD

    Joined:
    Jul 9, 2018
    Posts:
    30
    He people!
    I have a problem with Tiny:
    I create struct:

    upload_2019-2-6_16-32-35.png

    I have script who init and create pointers in object params:

    Code (JavaScript):
    1. static getEntity(world: ut.World, entity: string): ut.Entity {
    2.             return world.getEntityByName(entity);
    3.         }
    4.  
    5. ...
    6.  
    7. gameInterface.Buttons.NextPage = this.getEntity(world, "NextPage");
    8.             gameInterface.Buttons.PrevPage = this.getEntity(world, "PrevPage");
    9.             gameInterface.Buttons.ClosePopup = this.getEntity(world, "ClosePopup");
    10. gameInterface.Panels.Win = this.getEntity(world, "PanelWin");
    11.             gameInterface.Panels.Gamble = this.getEntity(world, "PanelGamble");
    12.             gameInterface.Panels.Paytable = this.getEntity(world, "PanelPaytable");
    13.             gameInterface.Panels.Popup = this.getEntity(world, "PanelPopup");
    14.  
    15.  
    If i use method to disable on one of this objects (red color), console send me about error:
    upload_2019-2-6_16-36-8.png

    code:

    Code (JavaScript):
    1. static SetActive(world: ut.World, entity:ut.Entity, active:boolean): void {
    2.             if(active) {
    3.                 if (world.hasComponent(entity, ut.Disabled)) {
    4.                     world.removeComponent(entity, ut.Disabled)
    5.                 }
    6.             } else {
    7.                 if (!world.hasComponent(entity, ut.Disabled)) {
    8.                     world.addComponent(entity, ut.Disabled);
    9.                 }
    10.             }
    11.         }
    12.  
    13. ...
    14.  
    15. this.SetActive(world, gameInterface.Buttons.NextPage, false);
    16.             this.SetActive(world, gameInterface.Buttons.PrevPage, false);
    17.             this.SetActive(world, gameInterface.Buttons.ClosePopup, false);
    18. this.SetActive(world, gameInterface.Panels.Win, false);
    19.             this.SetActive(world, gameInterface.Panels.Gamble, false);
    20.  
    21. this.SetActive(world, gameInterface.Panels.Paytable, false);
    22.             this.SetActive(world, gameInterface.Panels.Popup, false);
    What the problem?
     
  2. TheFordeD

    TheFordeD

    Joined:
    Jul 9, 2018
    Posts:
    30
    Help me please...
     
  3. SzabolcsNagy

    SzabolcsNagy

    Joined:
    Jan 28, 2019
    Posts:
    2
    Hi, I'm just guessing here, as I'm a beginner too, but I think those Entity references are valid only during the scope of the update() method. So you cannot really cache entity references like that. (You can possibly create an entity with a component that holds all those references and then they would remain valid, OR you call that getter each time you try to access those entities.). E.g.

    gameInterface.Buttons.NextPage = world => this.getEntity(world, "NextPage");

    and later on use it like:
    this.SetActive(world, gameInterface.Buttons.NextPage(this.world), false);
     
  4. TheFordeD

    TheFordeD

    Joined:
    Jul 9, 2018
    Posts:
    30
    As I understand it, do you suggest that I create a so-called prefab in which there are child elements?
     
  5. patolino

    patolino

    Joined:
    May 31, 2015
    Posts:
    6
    What I found walking between forum posts and with my own experience was, the entity becomes invalid after being disabled, but you can still access him creating your own reference.

    Code (JavaScript):
    1. const { index, version } = entity
    2. const entityRef = new ut.Entity(index, version)
    Do not cache the entity given by Tiny, because the object will be reused internally and became invalid or will be pointing to another random stuff.
    Create a new reference while you disable and use this reference to re-enable.