Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Niks4Linux · Dec 05, 2014 at 02:30 PM · c#instantiate-game-object

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);
         }
     }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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 ;)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sessional · Dec 05, 2014 at 04:21 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges