How to draw many separated gizmos and insert game objects ?

I created a 2d array of gizmos like this

void OnDrawGizmos()
{
	for (int i = 4; i <= 8; i += 2) 
	{
		for (int j = 12; j >= -12; j -= 2) 
		{
			Gizmos.DrawWireSphere(new Vector3(j, i, -5f), 1);
		}	
	}
}

then I want to instantiate a prefab in each single gizmo , I did it like this

	foreach (Transform child in transform) 
	{
		GameObject tie_fighter = Instantiate(Resources.Load("tie-fighter"), child.transform.position, Quaternion.identity) as GameObject;	
		tie_fighter.transform.parent = child;
	}

The problem is that the prefabs are instantiated at only one position, there are no separate gizmos it is only one gizmo, all the drawn gizmos don’t have actual transform position. How can I solve this ??

  • EnemiesCreation: GameObject contains the Prefabs creation script.

  • EnemiesPositions: GameObject contains the Draw Gizmos script.

I fixed it by first instantiating in the EnemiesPositions object containing the script for gizmos, then instantiating the enemy prefab at the location of each gizmo:

private void EnemiesCreationProcess()
	{
		for (int i = 4; i <= 8; i += 2) 
		{
			for (int j = 12; j >= -12; j -= 3) 
			{
				GameObject gizmo = Instantiate(Resources.Load("EnemiesPositions"), new Vector3(j, i , -5f), Quaternion.identity) as GameObject;
				gizmo.transform.parent = this.transform;
			}	
		}

		foreach (Transform child in transform) 
		{
			GameObject tie_fighter = Instantiate(Resources.Load("tie-fighter"), child.transform.position, Quaternion.identity) as GameObject;	
			tie_fighter.transform.parent = child;
		}
	}

And at the EnemiesPositions Script I just add a function to draw a single gizmo

public class EnemiesPositions : MonoBehaviour 
{
	void OnDrawGizmos()
	{
		Gizmos.DrawWireSphere(this.transform.position, 1);
	}
}