Search Unity

Best way to randomly select an entity from a list?

Discussion in 'Project Tiny' started by SoerenBachNielsen, Jan 11, 2019.

  1. SoerenBachNielsen

    SoerenBachNielsen

    Joined:
    Dec 27, 2012
    Posts:
    38
    Hey!

    I'm having trouble finding anything in the docs/tutorials about a scenario I'm looking for.
    I want to have maybe a Configuration with an entity reference list, where I could drag&drop my entities from my project folder and then use that runtime to spawn them randomly.
    But this only works with entities in the scene hierarchy, and not those in my project.

    Any other way to do this, somehow? Cheers!
     
  2. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    157
    Let's presume you have a EntityGroup called ItemGroup. In that group you have X items.

    You could:
    1) Instantiate the Group ( probably hold on for later no need to instantiate the group multiple times)
    2) Choose a random number something like index = Math.floor( Math.random()*(group.length-1) )
    3) select the random item group[index]
    4) give item a position and other needed data.

    Note be sure to have the default values of the Items in the group be out of the camera view.
     
  3. SoerenBachNielsen

    SoerenBachNielsen

    Joined:
    Dec 27, 2012
    Posts:
    38
    Aaah, yes of course! Thank you, gonna have to try that :)