ok, this probably isnt the most ideal way to do it (I’m still learning) but I think it will do what you want.
1: create your ground (for testing I just used a plane)
2: put in an empty gameobject roughly where you want a cube to spawn. Label this gameobject SpawnPoint or something useful.
3. Copy it as many times as you want & move each of them to where you want.
4: Create another empty gameobject & move it offscreen somewhere. This will be sort of like your manager for your spawn points.
5: Attach this script to it.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SpawnCubes : MonoBehaviour
{
public List<Transform>spawnSpots = new List<Transform> ();
//List <SpawnCubes> cubes = new List<SpawnCubes>();
public int cubeCount = 5;
public GameObject cubePrefab;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
// manage spawning of cubes
if (CubeScript.cubes.Count < cubeCount) {
// find a random spawn spot
int spawnPos;
bool spotFree;
do {
spotFree = true;
spawnPos = Random.Range (0, spawnSpots.Count - 1);
// check for cubes on top of each other
foreach (CubeScript sc in CubeScript.cubes) {
if(sc.transform.position == spawnSpots [spawnPos].position)
spotFree = false;
}
}
while(spotFree==false);
// instantiate the cube in this spot
Instantiate (cubePrefab, spawnSpots [spawnPos].position, spawnSpots [spawnPos].rotation);
}
}
}
6: select the manager object & lock the inspector.
7: select all the spawnpoints (excluding the manager) & drag them into the inspector to the item labelled Spawn Spots. It will create the size of the list & drop in all the spawn points as elements in the list.
8: unlock the inspector.
9: create your cube. Prefab it & attach this script to the prefab
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CubeScript : MonoBehaviour {
static public List<CubeScript> cubes = new List<CubeScript>();
// Use this for initialization
void Start () {
cubes.Add (this);
}
// Update is called once per frame
void Update () {
}
}
10: Drag the cube prefab into the Spawn Holder in the label Cube Prefab
11: Save everything.
12: Do a whole stack of tests to make sure that 5 items are spawning & visible on screen (in case it still spawns items on top of each other).
13: In the inspector for the spawn manager there is a field for Cube Count. This is set in the cube spawn script & is public so you can change it in the inspector. I tested this with 18 spawn points & 5 cubes as it was nice & small to test with.
What the scripts do:
the CubeScript just creates a list & inserts each cube into the list when it is instantiated.
the SpawnCubes script does all the work. It holds a list of all the possible spawn points. When it runs it randomly picks a number from 0 to the count of the number of items in the list -1 (because the list starts at 0, not 1). It then checks if that position is occupied against each cube in the cube list created when the cubes spawn. If something is there it starts again. When it finds an empty spot it instantiates a cube, which in turn updates the list of cubes when it is created.
Hope this helps.
Edit: if I have left mention of trees in the script replace it with cubes, I hacked this from my old script & may have missed bits. I also know my code commenting is bad. Note I left the empty Update on the cubeScript. If the cubes do nothing you can delete it but I left it there in case you did want them doing something when they are on screen (e.g. moving).
Edit Edit: I used c# as I don’t know java & I tested this in unity using 3D objects. Try to replicate this in java if that is what you use as you will learn more. If you decide to just use them make sure you create the script as c#.