I'm trying to create 12 collectable objects in a random position, but I couldn't do it, so I need your help. it will be something like that in the picture, but each time in a different position. I will be grateful if you could help me
Random.Range( min, max) will get you a random number from min to max. Make one random for X, one random for Y, and put them into your spawned object's transform.position property.
Yes, it worked, but I have two more questions. if they are very close to each other, spawn somewhere else, can I check this. second, if there is an object in the spawned position, spawn to another position, can I check this. because i will have enemys to and i dont want my enemy and collectable object to spawn in same position
Hi @onurduha - to answer your other questions, IIRC, this live training was discussing those questions (even if the main topic was events and messaging): https://unity3d.com/learn/tutorials/topics/scripting/events-creating-simple-messaging-system
Yes this is easy: keep a list and check against them. For any number of objects up to a few hundred, just checking all previous will be easily performant enough to do at load time. You just need to decide how close is "too close" and check the distance to all other objects so far placed. Another way of obtaining a better distribution in your space is to only randomize one of the axes, and instead iterate the other axis from left to right with some spacing, like so: Code (csharp): for (float x = LeftSide; x <= RightSide; x += HorizontalSpacingAmount) { float y = Random.Range( LowestPoint, HighestPoint); Vector3 position = new Vector3( x, y); // TODO: use position to position your object after you instantiate it } The above code will give a nice even spacing left to right, yet be very vertically randomized. I'll leave you to define the five limit and step variables in the above description, as they are fairly descriptive. You can even get trickier and use the ordinal number of your spawn as a "tweener" between the left and right, like so (modifying the above code): Code (csharp): for (int i = 0; i < NumberOfSpawnedObjects; i++) { // alpha will go from 0.0f to 1.0f float alpha = (float)i / (NumberOfSpawnedObjects - 1); // alpha "drives" the x from left to right float x = Mathf.Lerp( LeftSide, RightSide, alpha); // continue with the Vector3 position = line from the first snippet } That's nice because it frees you from having to create a HorizontalSpacingAmount. If you increase the number of objects, it just packs them tighter, all nice and evenly horizontally.