I'm interested in learning how to spawn entities from prefabs (from a monobehaviour) at random locations in a scene on a key press. Similar to this video, I'd like to instantiate these objects whenever the player presses a button. Is there a way to randomize the position of the object each time a new one is created?


1 Answer 1


After you instantiate the entity, try:

Entity newEntity =  //TODO: use your newly instantiated entity
//probably set this once when the system is created, or at least outside the Entites.ForEach() loop, otherwise values will always be the same
Random ranGenerator = Random.CreateFromIndex(123456789);

float3 spawnBounds = new float3(10, 10, 10);
float3 ranSpawnPos = ranGenerator.NextFloat3(-spawnBounds, spawnBounds);
Translation ranSpawnTranslation = new Translation() {Value = ranSpawnPos};
EntityManager.SetComponentData(newEntity, ranSpawnTranslation);
a side note

In general, I would be careful about starting with DOTS stuff if you're new to Unity or programming. Not necessarily because it's harder, but just because there's less documentation out there so it'll be harder to learn and find resources to get over hurdles... that being said, DOTS is very fun :)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .