- Home /
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[i].texture, cardslist[i].number);
}
}
Answer by Lunatix · Dec 05, 2014 at 03:26 PM
I think your problem is that you messed up rows and columns ;) 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 :)
A tip: use more variables, your code will be more structured and more clear to read, producing less errors messing with your mind ;)
And just to expand on his tip: variable na$$anonymous$$g conventions can help to make the code more readable. It's not necessary but using the camel case for your code can help with readability.
$$anonymous$$uch of a program time is spent in maintenance of existing code so all that you can do for readability is helpful at a later date.
Answer by Sessional · Dec 05, 2014 at 04:19 PM
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.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How to make a seamless transition between movie clips? 2 Answers
C# multiple AI Waypoint tutorial 0 Answers