Hi.
I have 9 GameObjects in vertical(y) order

and they are destroyed(3 Objects at a time) after use.

and I want my script to detect which spot is empty and spawn GameObjects 1 by 1.

Here’s my script to see which spot is empty.(by checking y position is not overlaped)

it may be hard to understand because the rest of the script is too long to attach here…

43967-b.jpg

    public GameObject[] readyLumies;
    private Vector3[] tr;
	private int spawnIdx = 0;
    public GameObject[] lumiPrefab; //3 types of lumi characters.
	private float[] lumiys;

void Start()
{
    tr = new Vector3[9];
	tr[0] = lumiInfo[0].lumies.transform.position;
	tr[1] = lumiInfo[1].lumies.transform.position;
	tr[2] = lumiInfo[2].lumies.transform.position;
	tr[3] = lumiInfo[3].lumies.transform.position;
	tr[4] = lumiInfo[4].lumies.transform.position;
	tr[5] = lumiInfo[5].lumies.transform.position;
	tr[6] = lumiInfo[6].lumies.transform.position;
	tr[7] = lumiInfo[7].lumies.transform.position;
	tr[8] = lumiInfo[8].lumies.transform.position;
}

void Update()
{
    readyLumies = GameObject.FindGameObjectsWithTag("READY");
    int howMany = readyLumies.Length;
}

public void SpawnLumi()
	{

		spawnIdx = GetSpawn();
		int randomCount = Random.Range(0,3);
		lumiInfo[spawnIdx].lumies = (GameObject) Instantiate(lumiPrefab[randomCount],tr[spawnIdx], Quaternion.Euler (0,0,0));
		GameObject childLight = (GameObject) Instantiate(lumiLightPrefab , tr[spawnIdx], Quaternion.Euler(0,0,0));
		childLight.transform.parent = lumiInfo[spawnIdx].lumies.transform;
	}

    
	public int GetSpawn()
	{
		int spawn = -1;
		
		while (true)
		{
			spawn = (int) Random.Range(0, 9-howMany);
			float ys = tr[spawn].y; 
			lumiys = new float[9];
			lumiys[0] =  readyLumies[0].transform.position.y;
			lumiys[1] =  readyLumies[1].transform.position.y;
			lumiys[2] =  readyLumies[2].transform.position.y;
			lumiys[3] =  readyLumies[3].transform.position.y;
			lumiys[4] =  readyLumies[4].transform.position.y;
			lumiys[5] =  readyLumies[5].transform.position.y;

			if ( ys != lumiys[spawn] /*&& ys != lumiys[1] && ys != lumiys[2] && ys != lumiys[3] && ys != lumiys[4] && ys != lumiys[5]*/)
			{
				System.Array.Clear(lumiys,0,lumiys.Length);
				break;
			}
		}
		return spawn;
	}

the problem is it spawns only on first run and this error stops next spawn.

Array index is out of range.”

and it doesn’t really detect which spot is empty.(spawns on existing object)

bonus character appeares around every 9 sopts covering existing character.

someone please tell me anything wrong you find here.

thank you for reading anyways :slight_smile:


Finally it’s working.

I used foreach to compare position

and used List to store position so positions not to be repeated.

here’s the script just in case someone searches for same answer latter…

oh btw I’m still having one error that says “stack overflow execption” on Random.Range(0,3) line in Function SpawnLumi()

I’ve searched and it says a recursive method causes overflow error.

I think I should change recursive method to something else but I just can’t think of it.

any help please?

public class LumiInfo
	{
		public GameObject lumies;
	}
    
public LumiInfo[] lumiInfo;
public GameObject[] lumiPrefab;
public GameObject[] readyLumies;
private Vector3[] tr;
public List<int> ttt = new List<int>();

void Update()
{
   readyLumies = GameObject.FindGameObjectsWithTag("READY"); // to find objects that didn't make move yet.
}

public void SpawnLumi()
	{
		int t = Random.Range(0, 8);
		if(checkIfPosEmpty(tr[t]) && t != ttt.Find(i=> i>-1) && t != ttt.FindLast(i=> i>-1))
			{
				lumiInfo[t] = new LumiInfo();
				int randomCount = Random.Range(0,3);
				lumiInfo[t].lumies = (GameObject) Instantiate(lumiPrefab[randomCount],tr[t], Quaternion.Euler (0,0,0));
				lumiInfo[t].lumies.gameObject.tag = "READY";
				GameObject childLight = (GameObject) Instantiate(lumiLightPrefab , tr[t], Quaternion.Euler(0,0,0));
				childLight.transform.parent = lumiInfo[t].lumies.transform;
				ttt.Add(t);

				StartCoroutine(SpawnFFly());
				
			return;

			}else{
					SpawnLumi();				
			}
	
	}

	public bool checkIfPosEmpty(Vector3 targetPos)
		{ 
	          foreach(GameObject current in readyLumies)
				{
					if(current.transform.position == targetPos)
					return false;
				} 

					return true;

		}

make sure howMany is smaller than or equal to 9

array is of size 9 but no init for index 6,7,8 . Then you clear that unadress index ? check that by simply allocating a array of 7 and clearing an array of 7.

if GetSpawn is wrong then SpawnLumi is too , because is calling it.

@Crafterguy3x3 @Ericool
Thank you I tried those but not solving…

It works on very first try (the character appears at empty spot.)

but when I pick up the second Item,
error message shows.(array index is out of range)

why do you think it works only on first try?
is it something with clearing the array?