Unable to instantiate Objects One Below another.

I want to Instantiate game objects one below another but when I run my script it showing objects one after another,tried lots of ways but unable to achieve it.Whats wrong with my code ?
Following is my code -

void SpawnCards(){
		int cardsinrow = 4;
		int cardsincolumn = cardslist.Count/cardsinrow;
		if(cardslist.Count % cardsinrow > 0)
			cardsincolumn += 1;
		float spacebetweencards = .2f;
		for(int i=0; i<cardslist.Count; i++){
			GameObject mc = Instantiate(memorycard, new Vector3((i%cardsinrow+(i%cardsinrow*spacebetweencards))-(cardsinrow/2f)+spacebetweencards, 0, (i/cardsinrow+(i/cardsinrow*spacebetweencards))-(cardsincolumn/2f)+spacebetweencards), memorycard.transform.rotation) as GameObject;
			mc.GetComponentInChildren<MemoryCard>().SetMemorycard(cardslist_.texture, cardslist*.number);*_

* }*
* }*

I think your problem is that you messed up rows and columns :wink:
Give the variables another name and you will see:

var numberOfColumns = 4.0f; // Columns in one row

for (float i = 0; i < cardsList.Count; i++) {
	var rowIndex = (int)(i / numberOfColumns); // 7.0 / 4.0 = 1.75, cut off ".75" and you have row index "1" 
	var columnIndex = i % numberOfColumns;

I think thats your problem :slight_smile:

A tip: use more variables, your code will be more structured and more clear to read, producing less errors messing with your mind :wink:

Another alternative that I made use of when generating levels for a platform game is to put a child game object where I want the next object to spawn at and disabling/removing the colliders and renderers. I then attached a simple component to each parent that contains references to each game object that it has as connection points.

When creating the next object in the platform I use the simple component’s reference to the game object that I wish to use for connection and place the new object at that connection points location.

One thing to note about this method: if you have grouped your objects under an empty, you should make sure that 0,0,0 in relation to the empty is where you expect it to be - the platform might spawn in absurd locations otherwise.

As far as the previous poster: I would have to agree with his analysis. A good part of programming is maintenance, and it can be very complex to maintain code with so much going on in a single line of code, try extracting some of the math to a different variable and/or doing operations in different lines of code to help keep it simpler.